当我编译openvswitch-1.5.0时,我遇到了以下编译错误:

 gcc -Wstrict-prototypes -Wall -Wno-sign-compare -Wpointer-arith
     -Wdeclaration-after-statement -Wformat-security -Wswitch-enum -Wunused-parameter -Wstrict-aliasing -Wbad-function-cast -Wcast-align -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-field-initializers -Wno-override-init  -g -O2 -export-dynamic ***-lpthread***  -o utilities/ovs-dpctl utilities/ovs-dpctl.o lib/libopenvswitch.a
 /home/jyyoo/src/dpdk/build/lib/librte_eal.a
 /home/jyyoo/src/dpdk/build/lib/libethdev.a
 /home/jyyoo/src/dpdk/build/lib/librte_cmdline.a
 /home/jyyoo/src/dpdk/build/lib/librte_hash.a
 /home/jyyoo/src/dpdk/build/lib/librte_lpm.a
 /home/jyyoo/src/dpdk/build/lib/librte_mbuf.a
 /home/jyyoo/src/dpdk/build/lib/librte_ring.a
 /home/jyyoo/src/dpdk/build/lib/librte_mempool.a
 /home/jyyoo/src/dpdk/build/lib/librte_malloc.a -lrt -lm 
     /usr/bin/ld: /home/jyyoo/src/dpdk/build/lib/librte_eal.a(eal.o): undefined reference
     to symbol 'pthread_create@@GLIBC_2.2.5'
     /lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from 
     command line

如果我尝试查看libpthread的符号,看起来没有问题。

$ readelf -s /lib/x86_64-linux-gnu/libpthread.so.0 | grep pthread_create
   199: 0000000000008220  2814 FUNC    GLOBAL DEFAULT   13 pthread_create@@GLIBC_2.2.5
   173: 0000000000008220  2814 FUNC    LOCAL  DEFAULT   13 __pthread_create_2_1
   462: 0000000000008220  2814 FUNC    GLOBAL DEFAULT   13 pthread_create@@GLIBC_2.2

你能给我一些提示或指点吗?


当前回答

改用g++编译。在我的案例中,从gcc切换到g++是有效的。

其他回答

我发现了另一个案子,所以我认为你们都错了。

这是我所拥有的:

/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/bin/ld: eggtrayicon.o: undefined reference to symbol 'XFlush'
/usr/lib64/libX11.so.6: error adding symbols: DSO missing from command line

问题是,命令行中没有包含- lx11 -尽管libX11. exe文件。所以应该作为依赖项添加,因为在参数中还有GTK和GNOME库。

所以,对我来说,唯一的解释是,这条信息可能是为了帮助你,但它没有正确地做到这一点。这可能很简单:提供该符号的库没有添加到命令行。

请注意POSIX中有关链接的三个重要规则:

Dynamic libraries have defined dependencies, so only libraries from the top-dependency should be supplied in whatever order (although after the static libraries) Static libraries have just undefined symbols - it's up to you to know their dependencies and supply all of them in the command line The order in static libraries is always: requester first, provider follows. Otherwise you'll get undefined symbol message, just like when you forgot to add the library to the command line When you specify the library with -l<name>, you never know whether it will take lib<name>.so or lib<name>.a. The dynamic library is preferred, if found, and static libraries only can be enforced by compiler option - that's all. And whether you have any problems as above, it depends on whether you had static or dynamic libraries Well, sometimes the dependencies may be lacking in dynamic libraries :D

当我安装HPCC基准测试(包括HPL和其他一些基准测试)时,同样的事情也发生在我身上。我在构建脚本的编译器标志中添加了-lm,然后它成功编译。

我发现有时候链接程序抱怨的库并不是引起问题的那个库。也许有一个聪明的方法来解决问题,但这就是我所做的:

在link命令中注释掉所有链接的库。 清除所有的.o, .so等(通常Clean就足够了,但你可能想要运行一个递归的find + rm,或类似的东西)。 每次取消link命令中的一个库的注释,并根据需要重新排列顺序。

@peter karasev:我在CentOS7上的gcc 4.8.2 cmake项目中遇到了同样的问题。“target_link_libraries”部分中的库的顺序很重要。我猜cmake只是将列表原样传递给链接器,也就是说,它不会尝试并计算出正确的顺序。这是合理的——当你思考它时,cmake不知道正确的顺序是什么,直到链接成功完成。

如果您正在使用cmake和使用过的pthread,请尝试添加以下行

find_package(Threads)
target_link_libraries(${CMAKE_THREAD_LIBS_INIT})

请添加:CFLAGS=“-lrt” 和 LDFLAGS=“-lrt”