c++ - 为什么我必须为 LLVM 链接这些库两次?

标签 c++ linker llvm

我正在尝试编译示例 LLVM 程序。链接器步骤使用此命令。

llvm-config-3.2 --ldflags --libs

这会产生以下命令。

g++  -o bin/Debug/test-llvm obj/Debug/main.o   -L/usr/lib/llvm-3.2/lib  -lpthread -lffi -ldl -lm  (a boat load of LLVM libraries here)

但是,它无法链接。我收到这样的错误。

undefined reference to ffi_type_float

因此,我在末尾添加了 -lffi-ldl

g++  -o bin/Debug/test-llvm obj/Debug/main.o   -L/usr/lib/llvm-3.2/lib  -lpthread -lffi -ldl -lm  (a boat load of LLVM libraries here) -lffi -ldl

所以,是的,它们在命令中出现了两次……但它是这样工作的。为什么?它们在前面的论点中被明确引用。

最佳答案

-lffi-ldl 之后出现在命令行上的一个或多个库指的是在其中一个库中定义的符号。但是链接器已经完成扫描 libffilibdl 并且不会重新扫描它们以查找这些符号。通过在列表末尾重新列出库的名称,强制链接器再次扫描这些库,可以解决这种循环依赖。

更具可扩展性的解决方案是使用 --start-group archives --end-group 选项来列出要链接到的库。引用自 man page :

-( archives -)
--start-group archives --end-group The archives should be a list of archive files. They may be either explicit file names, or -l options.

The specified archives are searched repeatedly until no new undefined references are created. Normally, an archive is searched only once in the order that it is specified on the command line. If a symbol in that archive is needed to resolve an undefined symbol referred to by an object in an archive that appears later on the command line, the linker would not be able to resolve that reference. By grouping the archives, they all be searched repeatedly until all possible references are resolved.

Using this option has a significant performance cost. It is best to use it only when there are unavoidable circular references between two or more archives.

所以你的命令行看起来像这样:

g++ -o bin/Debug/test-llvm obj/Debug/main.o -L/usr/lib/llvm-3.2/lib --start-group -lpthread -lffi -ldl -lm ... --end-group

关于c++ - 为什么我必须为 LLVM 链接这些库两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17100703/

相关文章:

c++ - 将十进制数转换为有理数时的精度问题

c++ -::boost::spirit: +alnum_p 和连字符

c++ - 如何将文件内容识别为 ASCII 或二进制

HTML 下载链接

c - Libclang API 获取定义在不同文件中的函数定义

c++ - Typedef C++ 中的位域/掩码

c++ - 为什么我需要使用 CGAL 和 CMake 构建我的 C++ 程序?

c - 在目标文件中嵌入链接器依赖项?

clang - 如何在llvm中获取全局变量的实际值

clang - 你如何为自己组装一个交叉编译器?