title: CMake and Clang Tooling linking error (out-of-tree)
body: <p>I am trying to compile the <a href="http://clang.llvm.org/docs/RAVFrontendAction.html" rel="nofollow noreferrer">RecursiveASTVisitor example of Clang</a> using a CMake file.
  Building the project goes well, however, linking C++ executable fails with multiple undefined references to LLVM and Clang libraries.
  I am building the example outside of the LLVM/Clang source file tree.</p>
  <p>Here is my CMakeLists.txt (I am using <a href="https://github.com/woboq/woboq_codebrowser/tree/master/cmake/modules" rel="nofollow noreferrer">FindClang.cmake and FindLLVM.cmake from this project</a>):</p>
  <pre><code>cmake_minimum_required(VERSION 2.8.4)
  project(ifcount)
  
  set(CMAKE_MODULE_PATH
    ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules
    ${CMAKE_MODULE_PATH})
  
  message(STATUS &quot;CMAKE module path: ${CMAKE_MODULE_PATH}&quot;)
  
  find_path(LibClang_INCLUDE_DIR clang-c/Index.h)
  find_library(LIBCLANG_LIBRARY NAMES clang)
  
  find_package(LLVM REQUIRED)
  find_package(LLVM REQUIRED CONFIG)
  Find_Package(LLVM REQUIRED)
  include_directories(${LLVM_INCLUDE_DIR})
  link_directories(${LLVM_LIB_DIR})
  
  message(STATUS &quot;Found LLVM ${LLVM_PACKAGE_VERSION}&quot;)
  message(STATUS &quot;Using LLVMConfig.cmake in: ${LLVM_DIR}&quot;)
  
  find_package(Clang REQUIRED)
  find_package(Clang REQUIRED clangTooling libClang)
  include_directories(${CLANG_INCLUDE_DIRS})
  
  ###
  add_definitions(${LLVM_DEFINITIONS})
  add_definitions(${Clang_DEFINITIONS})
  
  add_definitions(&quot;-std=c++11&quot;)
  set(CMAKE_CXX_FLAGS &quot;${CMAKE_CXX_FLAGS} ${LLVM_COMPILE_FLAGS} -Wall -fno-rtti -g -std=c++11 -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS&quot;)
  
  set(SOURCE_FILES main.cpp)
  add_executable(ifcount ${SOURCE_FILES})
  
  llvm_map_components_to_libnames(llvm_libs support core irreader)
  
  target_link_libraries(ifcount ${llvm_libs} ${LLVM_LDFLAGS} ${CLANG_LIBS})
  </code></pre>
  <p>And here is the output:</p>
  <pre><code>-- The C compiler identification is Clang 3.6.0
  -- The CXX compiler identification is Clang 3.6.0
  -- Check for working C compiler: /usr/local/bin/clang
  -- Check for working C compiler: /usr/local/bin/clang -- works
  -- Detecting C compiler ABI info
  -- Detecting C compiler ABI info - done
  -- Check for working CXX compiler: /usr/local/bin/clang++
  -- Check for working CXX compiler: /usr/local/bin/clang++ -- works
  -- Detecting CXX compiler ABI info
  -- Detecting CXX compiler ABI info - done
  -- CMAKE module path: /home/adel/workspace/ifcount/cmake/modules
  -- LLVM CXX flags: -I/usr/local/include  -fPIC -fvisibility-inlines-hidden -Wall -W -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -ffunction-sections -fdata-sections   -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
  -- LLVM LD flags: -L/usr/local/lib-lrt -ldl -ltinfo -lpthread -lz
  -- LLVM core libs: -lLLVMLTO -lLLVMObjCARCOpts -lLLVMLinker -lLLVMipo -lLLVMVectorize -lLLVMBitWriter -lLLVMIRReader -lLLVMAsmParser -lLLVMXCoreDisassembler -lLLVMXCoreCodeGen -lLLVMXCoreDesc -lLLVMXCoreInfo -lLLVMXCoreAsmPrinter -lLLVMSystemZDisassembler -lLLVMSystemZCodeGen -lLLVMSystemZAsmParser -lLLVMSystemZDesc -lLLVMSystemZInfo -lLLVMSystemZAsmPrinter -lLLVMSparcDisassembler -lLLVMSparcCodeGen -lLLVMSparcAsmParser -lLLVMSparcDesc -lLLVMSparcInfo -lLLVMSparcAsmPrinter -lLLVMR600CodeGen -lLLVMR600Desc -lLLVMR600Info -lLLVMR600AsmPrinter -lLLVMPowerPCDisassembler -lLLVMPowerPCCodeGen -lLLVMPowerPCAsmParser -lLLVMPowerPCDesc -lLLVMPowerPCInfo -lLLVMPowerPCAsmPrinter -lLLVMNVPTXCodeGen -lLLVMNVPTXDesc -lLLVMNVPTXInfo -lLLVMNVPTXAsmPrinter -lLLVMMSP430CodeGen -lLLVMMSP430Desc -lLLVMMSP430Info -lLLVMMSP430AsmPrinter -lLLVMMipsDisassembler -lLLVMMipsCodeGen -lLLVMMipsAsmParser -lLLVMMipsDesc -lLLVMMipsInfo -lLLVMMipsAsmPrinter -lLLVMHexagonCodeGen -lLLVMHexagonAsmPrinter -lLLVMHexagonDesc -lLLVMHexagonInfo -lLLVMCppBackendCodeGen -lLLVMCppBackendInfo -lLLVMARMDisassembler -lLLVMARMCodeGen -lLLVMARMAsmParser -lLLVMARMDesc -lLLVMARMInfo -lLLVMARMAsmPrinter -lLLVMAArch64Disassembler l-LLVMAArch64CodeGen -lLLVMAArch64AsmParser -lLLVMAArch64Desc -lLLVMAArch64Info -lLLVMAArch64AsmPrinter -lLLVMAArch64Utils -lLLVMTableGen -lLLVMDebugInfo -lLLVMOption -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMX86Desc -lLLVMMCDisassembler -lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMJIT -lLLVMLineEditor -lLLVMMCAnalysis -lLLVMInstrumentation -lLLVMInterpreter -lLLVMCodeGen -lLLVMScalarOpts -lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMProfileData -lLLVMMCJIT -lLLVMTarget -lLLVMRuntimeDyld -lLLVMObject -lLLVMMCParser -lLLVMBitReader -lLLVMExecutionEngine -lLLVMMC -lLLVMCore -lLLVMSupport
  -- LLVM JIT libs: -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMX86Desc -lLLVMObject -lLLVMMCParser -lLLVMBitReader -lLLVMMCDisassembler -lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMJIT -lLLVMExecutionEngine -lLLVMCodeGen -lLLVMScalarOpts -lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMTarget -lLLVMMC -lLLVMCore -lLLVMSupport
  -- LLVM JIT objs: 
  -- Found LLVM: /usr/local/include
  -- Found LLVM 3.6.0svn
  -- Using LLVMConfig.cmake in: /usr/local/share/llvm/cmake
  -- Clang libs: /usr/local/lib/libclangFrontend.a/usr/local/lib/libclangDriver.a/usr/local/lib/libclangCodeGen.a/usr/local/lib/libclangSema.a/usr/local/lib/libclangAnalysis.a/usr/local/lib/libclangRewrite.a/usr/local/lib/libclangAST.a/usr/local/lib/libclangParse.a/usr/local/lib/libclangLex.a/usr/local/lib/libclangBasic.a/usr/local/lib/libclangARCMigrate.a/usr/local/lib/libclangEdit.a/usr/local/lib/libclangFrontendTool.a/usr/local/lib/libclangRewrite.a/usr/local/lib/libclangSerialization.a/usr/local/lib/libclangTooling.a/usr/local/lib/libclangStaticAnalyzerCheckers.a/usr/local/lib/libclangStaticAnalyzerCore.a/usr/local/lib/libclangStaticAnalyzerFrontend.a/usr/local/lib/libclangSema.a
  -- Found Clang: /usr/local/include
  -- Clang libs: /usr/local/lib/libclangFrontend.a/usr/local/lib/libclangDriver.a/usr/local/lib/libclangCodeGen.a/usr/local/lib/libclangSema.a/usr/local/lib/libclangAnalysis.a/usr/local/lib/libclangRewrite.a/usr/local/lib/libclangAST.a/usr/local/lib/libclangParse.a/usr/local/lib/libclangLex.a/usr/local/lib/libclangBasic.a/usr/local/lib/libclangARCMigrate.a/usr/local/lib/libclangEdit.a/usr/local/lib/libclangFrontendTool.a/usr/local/lib/libclangRewrite.a/usr/local/lib/libclangSerialization.a/usr/local/lib/libclangTooling.a/usr/local/lib/libclangStaticAnalyzerCheckers.a/usr/local/lib/libclangStaticAnalyzerCore.a/usr/local/lib/libclangStaticAnalyzerFrontend.a/usr/local/lib/libclangSema.a/usr/local/lib/libclangFrontend.a/usr/local/lib/libclangDriver.a/usr/local/lib/libclangCodeGen.a/usr/local/lib/libclangSema.a/usr/local/lib/libclangAnalysis.a/usr/local/lib/libclangRewrite.a/usr/local/lib/libclangAST.a/usr/local/lib/libclangParse.a/usr/local/lib/libclangLex.a/usr/local/lib/libclangBasic.a/usr/local/lib/libclangARCMigrate.a/usr/local/lib/libclangEdit.a/usr/local/lib/libclangFrontendTool.a/usr/local/lib/libclangRewrite.a/usr/local/lib/libclangSerialization.a/usr/local/lib/libclangTooling.a/usr/local/lib/libclangStaticAnalyzerCheckers.a/usr/local/lib/libclangStaticAnalyzerCore.a/usr/local/lib/libclangStaticAnalyzerFrontend.a/usr/local/lib/libclangSema.a
  -- Found Clang: /usr/local/include;/usr/local/include
  -- Configuring done
  -- Generating done
  -- Build files have been written to: /home/adel/workspace/ifcount/build
  Scanning dependencies of target ifcount
  [100%] Building CXX object CMakeFiles/ifcount.dir/main.cpp.o
  Linking CXX executable ifcount
  </code></pre>
  <p>And finally, the errors are too numerous, here are some of their input:</p>
  <pre><code>/usr/local/bin/../lib/libclangFrontend.a(CompilerInvocation.cpp.o): In function `llvm::opt::arg_iterator::arg_iterator(llvm::opt::Arg* const*, llvm::opt::ArgList const&amp;, llvm::opt::OptSpecifier, llvm::opt::OptSpecifier, llvm::opt::OptSpecifier)':
  CompilerInvocation.cpp:(.text._ZN4llvm3opt12arg_iteratorC2EPKPNS0_3ArgERKNS0_7ArgListENS0_12OptSpecifierES9_S9_[_ZN4llvm3opt12arg_iteratorC5EPKPNS0_3ArgERKNS0_7ArgListENS0_12OptSpecifierES9_S9_]+0x5c): undefined reference to `llvm::opt::arg_iterator::SkipToNextArg()'
  /usr/local/bin/../lib/libclangFrontend.a(CompilerInvocation.cpp.o): In function `llvm::opt::arg_iterator::operator++()':
  CompilerInvocation.cpp:(.text._ZN4llvm3opt12arg_iteratorppEv[_ZN4llvm3opt12arg_iteratorppEv]+0x26): undefined reference to `llvm::opt::arg_iterator::SkipToNextArg()'
  /usr/local/bin/../lib/libclangFrontend.a(CompilerInvocation.cpp.o): In function `llvm::opt::ArgList::hasArg(llvm::opt::OptSpecifier) const':
  CompilerInvocation.cpp:(.text._ZNK4llvm3opt7ArgList6hasArgENS0_12OptSpecifierE[_ZNK4llvm3opt7ArgList6hasArgENS0_12OptSpecifierE]+0x1c): undefined reference to `llvm::opt::ArgList::getLastArg(llvm::opt::OptSpecifier) const'
  /usr/local/bin/../lib/libclangFrontend.a(CompilerInvocation.cpp.o): In function `getOptimizationLevel(llvm::opt::ArgList&amp;, clang::InputKind, clang::DiagnosticsEngine&amp;)':
  CompilerInvocation.cpp:(.text._ZL20getOptimizationLevelRN4llvm3opt7ArgListEN5clang9InputKindERNS3_17DiagnosticsEngineE+0x8d): undefined reference to `llvm::opt::ArgList::getLastArg(llvm::opt::OptSpecifier) const'
  CompilerInvocation.cpp:(.text._ZL20getOptimizationLevelRN4llvm3opt7ArgListEN5clang9InputKindERNS3_17DiagnosticsEngineE+0xc6): undefined reference to `llvm::opt::Option::matches(llvm::opt::OptSpecifier) const'
  CompilerInvocation.cpp:(.text._ZL20getOptimizationLevelRN4llvm3opt7ArgListEN5clang9InputKindERNS3_17DiagnosticsEngineE+0xfe): undefined reference to `llvm::opt::Option::matches(llvm::opt::OptSpecifier) const'
  CompilerInvocation.cpp:(.text._ZL20getOptimizationLevelRN4llvm3opt7ArgListEN5clang9InputKindERNS3_17DiagnosticsEngineE+0x136): undefined reference to `llvm::opt::Option::matches(llvm::opt::OptSpecifier) const'
  /usr/local/bin/../lib/libclangFrontend.a(CompilerInvocation.cpp.o): In function `getOptimizationLevelSize(llvm::opt::ArgList&amp;)':
  CompilerInvocation.cpp:(.text._ZL24getOptimizationLevelSizeRN4llvm3opt7ArgListE+0x2a): undefined reference to `llvm::opt::ArgList::getLastArg(llvm::opt::OptSpecifier) const'
  CompilerInvocation.cpp:(.text._ZL24getOptimizationLevelSizeRN4llvm3opt7ArgListE+0x5f): undefined reference to `llvm::opt::Option::matches(llvm::opt::OptSpecifier) const'
  /usr/local/bin/../lib/libclangFrontend.a(CompilerInvocation.cpp.o): In function `addDiagnosticArgs(llvm::opt::ArgList&amp;, llvm::opt::OptSpecifier, llvm::opt::OptSpecifier, std::vector&lt;std::string, std::allocator&lt;std::string&gt; &gt;&amp;)':
  CompilerInvocation.cpp:(.text._ZL17addDiagnosticArgsRN4llvm3opt7ArgListENS0_12OptSpecifierES3_RSt6vectorISsSaISsEE+0x188): undefined reference to `llvm::opt::Option::matches(llvm::opt::OptSpecifier) const'
  /usr/local/bin/../lib/libclangFrontend.a(CompilerInvocation.cpp.o): In function `ParseAnalyzerArgs(clang::AnalyzerOptions&amp;, llvm::opt::ArgList&amp;, clang::DiagnosticsEngine&amp;)':
  CompilerInvocation.cpp:(.text._ZL17ParseAnalyzerArgsRN5clang15AnalyzerOptionsERN4llvm3opt7ArgListERNS_17DiagnosticsEngineE+0x5e): undefined reference to `llvm::opt::ArgList::getLastArg(llvm::opt::OptSpecifier) const'
  CompilerInvocation.cpp:(.text._ZL17ParseAnalyzerArgsRN5clang15AnalyzerOptionsERN4llvm3opt7ArgListERNS_17DiagnosticsEngineE+0x132): undefined reference to `llvm::opt::Arg::getAsString(llvm::opt::ArgList const&amp;) const'
  CompilerInvocation.cpp:(.text._ZL17ParseAnalyzerArgsRN5clang15AnalyzerOptionsERN4llvm3opt7ArgListERNS_17DiagnosticsEngineE+0x204): undefined reference to `llvm::opt::ArgList::getLastArg(llvm::opt::OptSpecifier) const'
  CompilerInvocation.cpp:(.text._ZL17ParseAnalyzerArgsRN5clang15AnalyzerOptionsERN4llvm3opt7ArgListERNS_17DiagnosticsEngineE+0x2d8): undefined reference to `llvm::opt::Arg::getAsString(llvm::opt::ArgList const&amp;) const'
  CompilerInvocation.cpp:(.text._ZL17ParseAnalyzerArgsRN5clang15AnalyzerOptionsERN4llvm3opt7ArgListERNS_17DiagnosticsEngineE+0x3aa): undefined reference to `llvm::opt::ArgList::getLastArg(llvm::opt::OptSpecifier) const'
  CompilerInvocation.cpp:(.text._ZL17ParseAnalyzerArgsRN5clang15AnalyzerOptionsERN4llvm3opt7ArgListERNS_17DiagnosticsEngineE+0x4fe): undefined reference to `llvm::opt::Arg::getAsString(llvm::opt::ArgList const&amp;) const'
  CompilerInvocation.cpp:(.text._ZL17ParseAnalyzerArgsRN5clang15AnalyzerOptionsERN4llvm3opt7ArgListERNS_17DiagnosticsEngineE+0x5d0): undefined reference to `llvm::opt::ArgList::getLastArg(llvm::opt::OptSpecifier) const'
  CompilerInvocation.cpp:(.text._ZL17ParseAnalyzerArgsRN5clang15AnalyzerOptionsERN4llvm3opt7ArgListERNS_17DiagnosticsEngineE+0x6e4): undefined reference to `llvm::opt::Arg::getAsString(llvm::opt::ArgList const&amp;) const'
  CompilerInvocation.cpp:(.text._ZL17ParseAnalyzerArgsRN5clang15AnalyzerOptionsERN4llvm3opt7ArgListERNS_17DiagnosticsEngineE+0x7b6): undefined reference to `llvm::opt::ArgList::getLastArg(llvm::opt::OptSpecifier) const'
  CompilerInvocation.cpp:(.text._ZL17ParseAnalyzerArgsRN5clang15AnalyzerOptionsERN4llvm3opt7ArgListERNS_17DiagnosticsEngineE+0x8aa): undefined reference to `llvm::opt::Arg::getAsString(llvm::opt::ArgList const&amp;) const'
  CompilerInvocation.cpp:(.text._ZL17ParseAnalyzerArgsRN5clang15AnalyzerOptionsERN4llvm3opt7ArgListERNS_17DiagnosticsEngineE+0xc3f): undefined reference to `llvm::opt::ArgList::getLastArgValue(llvm::opt::OptSpecifier, llvm::StringRef) const'
  </code></pre>
  <p>Any help or tips would be appreciated.</p>