在Ubuntu Linux机器上,g++似乎没有像它应该的那样链接库。我不能提供完整的源代码,但本质上它是一个创建pthread的小程序。它在另外两台Debian机器上编译得很好,但在Ubuntu机器上,它抱怨对pthread_create的引用未定义。
命令行如下:

g++ -I. -lpthread source_code.cpp -o program

为了调试它,我在strace下运行g++来查看它是如何查找libpthread的。当我这样做时,我没有看到任何对libpthread的引用。好像链接器甚至都没有试图搜索库。在进行编译的机器上,当open()在文件系统中搜索库时,我看到了几个调用。
当我单独运行ld时,它似乎可以毫无问题地找到libpthread,strace输出确认它正在按原样搜索库。我像这样分开查身份证:
user@machine:~/src$ ld -lpthread
ld: warning: cannot find entry symbol _start; not setting start address

为什么当ld看起来自己做得很好时,g++不能正确地搜索文件系统中的库呢?目前这件事有点棘手。
谢谢!

最佳答案

要在gcc中启用多线程,应该传递-pthread标志not-lpthread-
-P螺纹

       Add support for multithreading using the POSIX threads library.
       This option sets flags for both
       the preprocessor and linker.
       It does not affect the thread safety of object code produced by the
       compiler or that of libraries supplied with it.

关于c++ - G++无法链接某些库,但是单独使用ld可以找到库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28483892/

10-14 14:18
查看更多