简体   繁体   English

在Linux上针对clang / llvm库的链接总是失败

[英]linking against clang/llvm libraries on linux always fails

I am using Linux Mint 15 Olivia, and I installed the clang compiler and libclang-dev packages available in the repositories, they are version 3.2. 我正在使用Linux Mint 15 Olivia,并且安装了库中可用的clang编译器和libclang-dev软件包,它们是3.2版。

I am trying to compile and link the example from clang repository http://llvm.org/svn/llvm-project/cfe/tags/RELEASE_32/final/examples/clang-interpreter/main.cpp as you can see it's from clang version 3.2. 我正在尝试从clang存储库http://llvm.org/svn/llvm-project/cfe/tags/RELEASE_32/final/examples/clang-interpreter/main.cpp编译并链接示例,如您所见,它来自clang版本3.2。

When I compiled it with the command: 当我用命令编译它时:

$> clang++ `llvm-config --cflags` -c main.cpp

I got the file main.o which contains some undefined symbols to clang/llvm libraries. 我得到了文件main.o,其中包含一些对clang / llvm库的未定义符号。 I made sure it contains the symbols by using the nm command: 我使用nm命令确保它包含符号:

$> nm main.o

A symbol from clang would be 来自c的符号将是

_ZN5clang16CompilerInstanceC1Ev

Until now everything worked fine, until I tried to link the 'main.o' file with clang/llvm libraries. 到现在为止,一切工作正常,直到我尝试将'main.o'文件与clang / llvm库链接。 When I issue the command: 当我发出命令时:

$> clang++ `llvm-config --ldflags` main.o `llvm-config --libs`

It fails with the following output (I just put the first error to not clutter this post): 它失败,并显示以下输出(我只是将第一个错误放在不使这篇文章混乱的地方):

main.o: In function `main':
main.cpp:74: undefined reference to `clang::TextDiagnosticPrinter::TextDiagnosticPrinter(llvm::raw_ostream&, clang::DiagnosticOptions*, bool)'

Basically I get a whole bunch of undefined references to clang libraries. 基本上,我得到了很多对clang库的未定义引用。 I've tried researching on this and all I've found is that this has to do with the order you put the libraries flags on the command line. 我已经尝试过对此进行研究,发现的一切与您在命令行上放置库标志的顺序有关。 In addition to this, I also tried some other things: 除此之外,我还尝试了其他一些方法:

  1. I tried a bunch of compiler flags, changing the order in which the linker flags appear, they never worked. 我尝试了一堆编译器标志,更改了链接器标志的显示顺序,但它们从未起作用。
  2. I downloaded and compiled the LLVM and clang source code version 3.2, and 3.4, same result: compiling works, linking fails. 我下载并编译了LLVM和clang源代码版本3.2和3.4,结果相同:编译有效,链接失败。 It's worth mentioning to say that for each version reordering the linker flags always gave different link errors (this is of course due to the way the link searches for libraries). 值得一提的是,对于每个版本,对链接器标志进行重新排序总是会产生不同的链接错误(这当然是由于链接搜索库的方式所致)。

I ran out of ideas, and already spent 2 hours trying to compile a simple example from clang's repository, any help would be appreciated. 我没有想法,已经花了两个小时尝试从clang的存储库中编译一个简单的示例,任何帮助将不胜感激。

Thank you 谢谢

The answer is easy - llvm-config will not give you clang libraries. 答案很简单-llvm-config不会给您clang库。 You need to link them separately. 您需要分别链接它们。 Check clang/tools/driver/Makefile as an example of a library list. 检查clang / tools / driver / Makefile作为库列表的示例。

The answer to this problem is as Anton Korobeynik suggested, I was missing the clang libraries (which are not part of the llvm build as I was expecting from the command 'llvm-config --libs'). 正如安东·科罗贝尼克(Anton Korobeynik)所建议的那样,该问题的答案是,我缺少了clang库(如我从命令“ llvm-config --libs”所期望的那样,它们不属于llvm构建)。

In the end the final command turned out to be: 最后,最后的命令结果是:

 clang++ `llvm-config --ldflags` main.o  -lclangFrontendTool -lclangFrontend -lclangDriver -lclangSerialization -lclangCodeGen -lclangParse -lclangSema -lclangStaticAnalyzerFrontend -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangAnalysis -lclangARCMigrate -lclangRewriteFrontend -lclangRewriteCore -lclangEdit -lclangAST -lclangLex -lclangBasic `llvm-config --libs`

If compiling any clang tool or example, make sure you check the Makefiles under clang/tools folder :) 如果编译任何clang工具或示例,请确保检查clang / tools文件夹下的Makefile:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM