How to link against lib/libclang in developing a clang-tools-extra project?

Hi,

I’m writing a small clang tool in the clang-tools-extra directory by using the monorepo. My CMakeList.txt files is as follows:

set(LLVM_LINK_COMPONENTS Support)

add_clang_executable(loop-convert
LoopConvert.cpp
)

target_link_libraries(loop-convert
PRIVATE
clang
clangAST
clangASTMatchers
clangBasic
clangFrontend
clangTooling
)

When I ran the build command, I got a linker error complaining that functions in lib/libclang.so cannot be found:

$ ninja loop-convert
[1/1] Linking CXX executable bin/loop-convert
FAILED: : && /usr/bin/clang++-6.0 -fPIC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -std=c++11 -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wstring-conversion -fdiagnostics-color -fno-common -Woverloaded-virtual -Wno-nested-anon-types -g -fuse-ld=lld-6.0 -Wl,--color-diagnostics -Wl,-allow-shlib-undefined tools/clang/tools/extra/loop-convert/CMakeFiles/loop-convert.dir/LoopConvert.cpp.o -o bin/loop-convert lib/libLLVMSupport.so.8svn -lpthread lib/libclangAST.so.8svn lib/libclangASTMatchers.so.8svn lib/libclangBasic.so.8svn lib/libclangFrontend.so.8svn lib/libclangTooling.so.8svn -Wl,-rpath,"\$ORIGIN/../lib" && :
/usr/bin/ld.lld-6.0: error: undefined symbol: clang_getCString
>>> referenced by LoopConvert.cpp:7 (/home/lu/work/git-repos/llvm-mono/llvm-project/clang-tools-extra/loop-convert/LoopConvert.cpp:7)
>>> tools/clang/tools/extra/loop-convert/CMakeFiles/loop-convert.dir/LoopConvert.cpp.o:(operator<<(std::ostream&, CXString const&))

...

It is clear that the lib/libclang.so.8svn is missing from the command line invoked by ninja. How can I add it into the build command? The “clang” line in the CMakeList.txt file doesn’t work.

Thanks.

Hmm, try adding “libclang” the same way you added clangFrontend! No idea whether that’ll work, but after looking around, that’s how other cmake files seem to do it.

Lou Wynn via cfe-dev <cfe-dev@lists.llvm.org> ezt írta (időpont: 2018. okt. 25., Cs, 5:13):

That works. “clang” itself doesn’t. Thank you!