如何使用clang ++编译?

时间:2015-10-21 08:01:14

标签: c++ macos clang llvm clang++

我正在构建一个简单的解析器(用c ++编写以扩展clang AST)基于clang& llvm在OS X优胜美地。我在我的主目录中下载了clang和llvm源代码(~/clang-llvm/llvm是源代码,~/clang-llvm/build是构建目录)并使用cmake&使

例如,示例解析器包括clang源文件,如:

#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "llvm/Support/raw_ostream.h"

使用clang的AST匹配功能。但是当我尝试使用clang ++编译它时,我得到如下错误

Undefined symbols for architecture x86_64:
  "llvm::RefCountedBaseVPTR::anchor()", referenced from:
      vtable for clang::ast_matchers::internal::matcher_isPublicMatcher in matchers_rewriter-b01682.o
      vtable for clang::ast_matchers::internal::matcher_isProtectedMatcher in matchers_rewriter-b01682.o
      vtable for clang::ast_matchers::internal::matcher_isPrivateMatcher in matchers_rewriter-b01682.o
      vtable for clang::ast_matchers::internal::matcher_isImplicitMatcher in matchers_rewriter-b01682.o
      vtable for clang::ast_matchers::internal::matcher_ignoringImpCasts0Matcher in matchers_rewriter-b01682.o
      vtable for clang::ast_matchers::internal::matcher_ignoringParenCasts0Matcher in matchers_rewriter-b01682.o
      vtable for clang::ast_matchers::internal::matcher_ignoringParenImpCasts0Matcher in matchers_rewriter-b01682.o
      ...
  "llvm::cl::OptionCategory::registerCategory()", referenced from:
      llvm::cl::OptionCategory::OptionCategory(char const*, char const*) in matchers_rewriter-b01682.o
  "llvm::outs()", referenced from:

<<OMITTED>>

"typeinfo for clang::ASTFrontendAction", referenced from:
      typeinfo for MyFrontendAction in matchers_rewriter-b01682.o
  "typeinfo for clang::tooling::FrontendActionFactory", referenced from:
      typeinfo for std::__1::unique_ptr<clang::tooling::FrontendActionFactory, std::__1::default_delete<clang::tooling::FrontendActionFactory> > clang::tooling::newFrontendActionFactory<MyFrontendAction>()::SimpleFrontendActionFactory in matchers_rewriter-b01682.o
  "vtable for llvm::RefCountedBaseVPTR", referenced from:
      llvm::RefCountedBaseVPTR::RefCountedBaseVPTR() in matchers_rewriter-b01682.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for clang::ASTConsumer", referenced from:
      clang::ASTConsumer::ASTConsumer() in matchers_rewriter-b01682.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for clang::ast_matchers::MatchFinder::MatchCallback", referenced from:
      clang::ast_matchers::MatchFinder::MatchCallback::MatchCallback() in matchers_rewriter-b01682.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for clang::ASTFrontendAction", referenced from:
      clang::ASTFrontendAction::ASTFrontendAction() in matchers_rewriter-b01682.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for clang::tooling::ToolAction", referenced from:
      clang::tooling::ToolAction::ToolAction() in matchers_rewriter-b01682.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for clang::tooling::FrontendActionFactory", referenced from:
      clang::tooling::FrontendActionFactory::FrontendActionFactory() in matchers_rewriter-b01682.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我使用了以下编译选项

clang++ -fno-rtti -O0 -std=c++11 -g -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS `/Users/me/clang-llvm/build/Debug+Asserts/bin/llvm-config --cxxflags`

编译器似乎需要.o或.a文件的clang&amp; LLVM。我是否需要提供更多选项或指定任何路径?我尝试在编译文件存在的地方添加-I /Users/me/clang-llvm/build/Debug+Asserts/lib,但它也不起作用。

1 个答案:

答案 0 :(得分:1)

我希望你能得到一个更好的答案,因为据我所知,llvm-config工具已被破坏。

我只能通过链接我在clang lib 文件夹(我平台上的/usr/lib64/llvm/)中找到的所有clang 静态库来实现这一点。

CLANG_LIBS = \
    -L/usr/lib64/llvm \
    -lclang \
    -llldb \
    -lLLVM-3.5 \
    /usr/lib64/llvm/libclangFrontend.a \
    /usr/lib64/llvm/libllvm.a \
    /usr/lib64/llvm/libLLVMInstrumentation.a \
    /usr/lib64/llvm/libLLVMAArch64Desc.a \
    /usr/lib64/llvm/libclangASTMatchers.a \
    /usr/lib64/llvm/libLLVMRuntimeDyld.a \
    /usr/lib64/llvm/libLLVMLTO.a \
    /usr/lib64/llvm/liblldbUtility.a \
    /usr/lib64/llvm/libLLVMR600Info.a \
    /usr/lib64/llvm/libllvm_R600.a \
    /usr/lib64/llvm/libllvm_bitwriter.a \
    /usr/lib64/llvm/libLLVMAsmParser.a \
    /usr/lib64/llvm/libLLVMTransformUtils.a \
    /usr/lib64/llvm/libLLVMR600CodeGen.a \
    /usr/lib64/llvm/liblldbPluginPlatformFreeBSD.a \
    /usr/lib64/llvm/liblldbHostLinux.a \
    /usr/lib64/llvm/liblldbPluginPlatformKalimba.a \
    /usr/lib64/llvm/libLLVMX86CodeGen.a \
    /usr/lib64/llvm/liblldbPluginJITLoaderGDB.a \
    /usr/lib64/llvm/libLLVMARMInfo.a \
    /usr/lib64/llvm/libLLVMR600Desc.a \
    /usr/lib64/llvm/libLLVMX86Disassembler.a \
    /usr/lib64/llvm/libclangTooling.a \
    /usr/lib64/llvm/libLLVMNVPTXAsmPrinter.a \
    /usr/lib64/llvm/libLLVMMCDisassembler.a \
    /usr/lib64/llvm/liblldbPluginABISysV_x86_64.a \
    /usr/lib64/llvm/liblldbInterpreter.a \
    /usr/lib64/llvm/liblldbPluginObjectContainerBSDArchive.a \
    /usr/lib64/llvm/liblldbCore.a \
    /usr/lib64/llvm/libLLVMNVPTXInfo.a \
    /usr/lib64/llvm/libLLVMMC.a \
    /usr/lib64/llvm/libLLVMAArch64Utils.a \
    /usr/lib64/llvm/liblldbPluginLanguageRuntimeObjCAppleObjCRuntime.a \
    /usr/lib64/llvm/libclangFrontendTool.a \
    /usr/lib64/llvm/libLLVMObject.a \
    /usr/lib64/llvm/liblldbBreakpoint.a \
    /usr/lib64/llvm/liblldbPluginPlatformWindows.a \
    /usr/lib64/llvm/libLLVMAnalysis.a \
    /usr/lib64/llvm/libLLVMSystemZDisassembler.a \
    /usr/lib64/llvm/libLLVMNVPTXCodeGen.a \
    /usr/lib64/llvm/libllvm_analysis.a \
    /usr/lib64/llvm/libLLVMInterpreter.a \
    /usr/lib64/llvm/libclang.a \
    /usr/lib64/llvm/libclangRewrite.a \
    /usr/lib64/llvm/libLLVMARMAsmParser.a \
    /usr/lib64/llvm/liblldbCommands.a \
    /usr/lib64/llvm/liblldbPluginSymbolFileSymtab.a \
    /usr/lib64/llvm/libclangDynamicASTMatchers.a \
    /usr/lib64/llvm/liblldbPluginProcessPOSIX.a \
    /usr/lib64/llvm/libclangSema.a \
    /usr/lib64/llvm/liblldbPluginProcessElfCore.a \
    /usr/lib64/llvm/liblldbHostCommon.a \
    /usr/lib64/llvm/libllvm_scalar_opts.a \
    /usr/lib64/llvm/libLLVMLineEditor.a \
    /usr/lib64/llvm/libllvm_irreader.a \
    /usr/lib64/llvm/liblldbPluginPlatformPOSIX.a \
    /usr/lib64/llvm/libLLVMAArch64AsmPrinter.a \
    /usr/lib64/llvm/libLLVMScalarOpts.a \
    /usr/lib64/llvm/libLLVMX86Desc.a \
    /usr/lib64/llvm/libLLVMX86AsmPrinter.a \
    /usr/lib64/llvm/libclangBasic.a \
    /usr/lib64/llvm/libclangFormat.a \
    /usr/lib64/llvm/libLLVMLinker.a \
    /usr/lib64/llvm/liblldbSymbol.a \
    /usr/lib64/llvm/libLLVMMCParser.a \
    /usr/lib64/llvm/liblldbPluginABIMacOSX_arm64.a \
    /usr/lib64/llvm/libLLVMPowerPCDisassembler.a \
    /usr/lib64/llvm/liblldbPluginEmulateInstructionARM.a \
    /usr/lib64/llvm/libclangEdit.a \
    /usr/lib64/llvm/libLLVMARMCodeGen.a \
    /usr/lib64/llvm/libLLVMPowerPCInfo.a \
    /usr/lib64/llvm/libLLVMipo.a \
    /usr/lib64/llvm/libLLVMCore.a \
    /usr/lib64/llvm/libllvm_passmgr_builder.a \
    /usr/lib64/llvm/libLLVMARMAsmPrinter.a \
    /usr/lib64/llvm/libllvm_executionengine.a \
    /usr/lib64/llvm/liblldbPluginDynamicLoaderPOSIX.a \
    /usr/lib64/llvm/libLLVMARMDisassembler.a \
    /usr/lib64/llvm/liblldbPluginProcessGDBRemote.a \
    /usr/lib64/llvm/libllvm_X86.a \
    /usr/lib64/llvm/liblldbPluginABIMacOSX_arm.a \
    /usr/lib64/llvm/liblldbAPI.a \
    /usr/lib64/llvm/libLLVMAsmPrinter.a \
    /usr/lib64/llvm/libclangCodeGen.a \
    /usr/lib64/llvm/libclangDriver.a \
    /usr/lib64/llvm/libLLVMExecutionEngine.a \
    /usr/lib64/llvm/liblldbExpression.a \
    /usr/lib64/llvm/liblldbPluginPlatformLinux.a \
    /usr/lib64/llvm/libllvm_vectorize.a \
    /usr/lib64/llvm/libLLVMX86Utils.a \
    /usr/lib64/llvm/liblldbPluginObjectFilePECOFF.a \
    /usr/lib64/llvm/liblldbPluginDynamicLoaderStatic.a \
    /usr/lib64/llvm/libLLVMMCAnalysis.a \
    /usr/lib64/llvm/libLLVMPowerPCAsmParser.a \
    /usr/lib64/llvm/libLLVMipa.a \
    /usr/lib64/llvm/liblldbPluginDynamicLoaderHexagon.a \
    /usr/lib64/llvm/libclangParse.a \
    /usr/lib64/llvm/libLLVMNVPTXDesc.a \
    /usr/lib64/llvm/libLLVMAArch64AsmParser.a \
    /usr/lib64/llvm/libclangLex.a \
    /usr/lib64/llvm/libLLVMPowerPCDesc.a \
    /usr/lib64/llvm/liblldbPluginPlatformGDBServer.a \
    /usr/lib64/llvm/libLLVMJIT.a \
    /usr/lib64/llvm/libLLVMAArch64Disassembler.a \
    /usr/lib64/llvm/libllvm_target.a \
    /usr/lib64/llvm/liblldbPluginDynamicLoaderMacOSX.a \
    /usr/lib64/llvm/liblldbPluginUnwindAssemblyInstEmulation.a \
    /usr/lib64/llvm/libLLVMBitWriter.a \
    /usr/lib64/llvm/libLLVMSelectionDAG.a \
    /usr/lib64/llvm/libLLVMOption.a \
    /usr/lib64/llvm/libLLVMSystemZCodeGen.a \
    /usr/lib64/llvm/libclangStaticAnalyzerCheckers.a \
    /usr/lib64/llvm/libllvm_AArch64.a \
    /usr/lib64/llvm/libllvm_ARM.a \
    /usr/lib64/llvm/libLLVMR600AsmPrinter.a \
    /usr/lib64/llvm/libLLVMSystemZInfo.a \
    /usr/lib64/llvm/libLLVMAArch64CodeGen.a \
    /usr/lib64/llvm/libLLVMSystemZAsmParser.a \
    /usr/lib64/llvm/libLLVMCppBackendCodeGen.a \
    /usr/lib64/llvm/libllvm_bitreader.a \
    /usr/lib64/llvm/libLLVMBitReader.a \
    /usr/lib64/llvm/libLLVMIRReader.a \
    /usr/lib64/llvm/libclangIndex.a \
    /usr/lib64/llvm/libLLVMObjCARCOpts.a \
    /usr/lib64/llvm/libLLVMPowerPCAsmPrinter.a \
    /usr/lib64/llvm/libLLVMCppBackendInfo.a \
    /usr/lib64/llvm/libLLVMAArch64Info.a \
    /usr/lib64/llvm/libllvm_CppBackend.a \
    /usr/lib64/llvm/liblldbPluginObjectFileJIT.a \
    /usr/lib64/llvm/libLLVMMCJIT.a \
    /usr/lib64/llvm/liblldbDataFormatters.a \
    /usr/lib64/llvm/liblldbPluginABIMacOSX_i386.a \
    /usr/lib64/llvm/libLLVMX86Info.a \
    /usr/lib64/llvm/libLTO.a \
    /usr/lib64/llvm/liblldbPluginLanguageRuntimeCPlusPlusItaniumABI.a \
    /usr/lib64/llvm/libclangStaticAnalyzerFrontend.a \
    /usr/lib64/llvm/libLLVMX86AsmParser.a \
    /usr/lib64/llvm/liblldbPluginObjectFileELF.a \
    /usr/lib64/llvm/libclangAST.a \
    /usr/lib64/llvm/libllvm_ipo.a \
    /usr/lib64/llvm/libLLVMProfileData.a \
    /usr/lib64/llvm/libLLVMSystemZDesc.a \
    /usr/lib64/llvm/libLLVMPowerPCCodeGen.a \
    /usr/lib64/llvm/libclangAnalysis.a \
    /usr/lib64/llvm/libLLVMSupport.a \
    /usr/lib64/llvm/liblldbPluginProcessLinux.a \
    /usr/lib64/llvm/liblldbTarget.a \
    /usr/lib64/llvm/libclangARCMigrate.a \
    /usr/lib64/llvm/libLLVMTarget.a \
    /usr/lib64/llvm/liblldbPluginUnwindAssemblyx86.a \
    /usr/lib64/llvm/libLLVMSystemZAsmPrinter.a \
    /usr/lib64/llvm/liblldbPluginUtility.a \
    /usr/lib64/llvm/libclangStaticAnalyzerCore.a \
    /usr/lib64/llvm/libclangSerialization.a \
    /usr/lib64/llvm/libLLVMVectorize.a \
    /usr/lib64/llvm/libclangRewriteFrontend.a \
    /usr/lib64/llvm/liblldbPluginOperatingSystemPython.a \
    /usr/lib64/llvm/libllvm_linker.a \
    /usr/lib64/llvm/liblldbInitAndLog.a \
    /usr/lib64/llvm/libllvm_all_backends.a \
    /usr/lib64/llvm/liblldbPluginDisassemblerLLVM.a \
    /usr/lib64/llvm/libllvm_NVPTX.a \
    /usr/lib64/llvm/libLLVMInstCombine.a \
    /usr/lib64/llvm/libLLVMDebugInfo.a \
    /usr/lib64/llvm/liblldbPluginSymbolFileDWARF.a \
    /usr/lib64/llvm/liblldbPluginABISysV_hexagon.a \
    /usr/lib64/llvm/libLLVMTableGen.a \
    /usr/lib64/llvm/liblldbPluginEmulateInstructionARM64.a \
    /usr/lib64/llvm/libllvm_PowerPC.a \
    /usr/lib64/llvm/libllvm_SystemZ.a \
    /usr/lib64/llvm/liblldbPluginSymbolVendorELF.a \
    /usr/lib64/llvm/libLLVMARMDesc.a \
    /usr/lib64/llvm/liblldbPluginPlatformMacOSX.a \
    /usr/lib64/llvm/libLLVMCodeGen.a

我很想找到一个合适的解决方案。