我按照这里的教程一直在玩clang / llvm 3.0 http://amnoid.de/tmp/clangtut/tut.html
本教程中的代码有点过时但经过一些修改后,编译好了(在Ubuntu上使用clang ++ / g ++):
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/Host.h>
#include <llvm/ADT/IntrusiveRefCntPtr.h>
#include <clang/Frontend/DiagnosticOptions.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Basic/Diagnostic.h>
#include <clang/Basic/TargetOptions.h>
#include <clang/Basic/TargetInfo.h>
#include <clang/Basic/FileManager.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/Lex/HeaderSearch.h>
using namespace clang;
int main()
{
DiagnosticOptions diagOpt;
TextDiagnosticPrinter *pTextDiagPrinter = new TextDiagnosticPrinter(llvm::outs(), diagOpt);
llvm::IntrusiveRefCntPtr<DiagnosticIDs> pDiagIds(new DiagnosticIDs);
DiagnosticsEngine *pDiagEng = new DiagnosticsEngine(pDiagIds, pTextDiagPrinter);
Diagnostic diag(pDiagEng);
LangOptions lang;
FileSystemOptions fmOpt;
FileManager fm(fmOpt);
SourceManager sm(*pDiagEng, fm);
HeaderSearch headers(fm);
TargetOptions tgtOpt;
tgtOpt.Triple = llvm::sys::getHostTriple();
TargetInfo *ti = TargetInfo::CreateTargetInfo(*pDiagEng, tgtOpt);
CompilerInstance comp;
Preprocessor pp(*pDiagEng, lang, ti, sm, headers, comp);
return 0;
}
问题在于链接,我猜问题是库顺序,尝试了各种组合,有些给出了数千个未定义的符号,我得到的最好就是:
g++ -fno-exceptions -fno-rtti -fno-common \
-lclangParse -lclangSerialization -lclangDriver -lclangSema -lclangAnalysis -lclangAST -lclangFrontend \
-lclangLex -lclangBasic -lclang \
`llvm-config --cxxflags --ldflags --libs` -ldl tut1.cpp
给出了
tut1.cpp:(.text.startup+0xca): undefined reference to `llvm::outs()'
tut1.cpp:(.text.startup+0xef): undefined reference to `clang::TextDiagnosticPrinter::TextDiagnostic
Printer(llvm::raw_ostream&, clang::DiagnosticOptions const&, bool)'
tut1.cpp:(.text.startup+0x104): undefined reference to `clang::DiagnosticIDs::DiagnosticIDs()'
tut1.cpp:(.text.startup+0x13d): undefined reference to `clang::DiagnosticsEngine::DiagnosticsEngine
(llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> const&, clang::DiagnosticConsumer*, bool)'
tut1.cpp:(.text.startup+0x14a): undefined reference to `clang::LangOptions::LangOptions()'
tut1.cpp:(.text.startup+0x167): undefined reference to `clang::FileManager::FileManager(clang::File
SystemOptions const&)'
tut1.cpp:(.text.startup+0x17f): undefined reference to `clang::SourceManager::SourceManager(clang::
DiagnosticsEngine&, clang::FileManager&)'
tut1.cpp:(.text.startup+0x194): undefined reference to `clang::HeaderSearch::HeaderSearch(clang::Fi
leManager&)'
tut1.cpp:(.text.startup+0x1f5): undefined reference to `llvm::sys::getHostTriple()'
tut1.cpp:(.text.startup+0x227): undefined reference to `clang::TargetInfo::CreateTargetInfo(clang::
DiagnosticsEngine&, clang::TargetOptions&)'
tut1.cpp:(.text.startup+0x232): undefined reference to `clang::CompilerInstance::CompilerInstance()
'
tut1.cpp:(.text.startup+0x277): undefined reference to `clang::Preprocessor::Preprocessor(clang::Di
agnosticsEngine&, clang::LangOptions&, clang::TargetInfo const*, clang::SourceManager&, clang::Head
erSearch&, clang::ModuleLoader&, clang::IdentifierInfoLookup*, bool, bool)'
tut1.cpp:(.text.startup+0x281): undefined reference to `clang::Preprocessor::~Preprocessor()'
tut1.cpp:(.text.startup+0x289): undefined reference to `clang::CompilerInstance::~CompilerInstance(
)'
tut1.cpp:(.text.startup+0x2e4): undefined reference to `clang::HeaderSearch::~HeaderSearch()'
tut1.cpp:(.text.startup+0x2f1): undefined reference to `clang::SourceManager::~SourceManager()'
tut1.cpp:(.text.startup+0x2fe): undefined reference to `clang::FileManager::~FileManager()'
tut1.cpp:(.text.startup+0x345): undefined reference to `clang::DiagnosticIDs::~DiagnosticIDs()'
collect2: ld returned 1 exit status
我也关注此链接Linking against clang-llvm 但似乎它不适用于clang 3.0。
任何人都知道如何成功构建代码,或者更好的是,某些工具如某些clang-config?
答案 0 :(得分:3)
只需在.o文件之后使用LLVM标志,除非将目标文件链接起来 标志写在它之后。一个简单的makefile来显示我的意思
CC=clang++
LFLAGS=`llvm-config --cppflags --ldflags --libs core`
all:
$(CC) -o out in.cc $(FLAGS)
答案 1 :(得分:0)
clang
驱动程序本身链接:
-lclangFrontendTool -lclangFrontend -lclangDriver -lclangSerialization -lclangCodeGen -lclangParse -lclangSema -lclangStaticAnalyzerFrontend -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangAnalysis -lclangIndex -lclangARCMigrate -lclangRewrite -lclangAST -lclangLex -lclangBasic
如何找到它的一个小技巧:构建clang,删除clang二进制文件,然后运行make -n
。
答案 2 :(得分:0)
使用静态库时,某些链接器对-l *标志的顺序很敏感。 clang库似乎没问题,所以尝试用clang库列表交换`llvm-config --cxxflags --ldflags --libs`。
如果没有帮助,只有@ SK-logic建议的解决方案 - 运行make -n
并查看clang target的编译命令。