g ++链接到外部库会创建未定义的引用

时间:2017-10-03 15:19:56

标签: c++ linux gcc

我正在尝试构建一个简单的程序来测试外部库的使用,但是在将它与g ++链接时遇到了麻烦。见命令/结果:

user@user-Nuvo-2510VTC:~/Desktop/WDT_DIO/linux/test$ g++ -o main main.o -lwdt_dio
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `sem_unlink'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_mutexattr_settype'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `sem_close'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_spin_lock'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_spin_unlock'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_create'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_spin_init'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_mutexattr_init'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_spin_destroy'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `sem_post'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `sem_open'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `sem_getvalue'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `sem_wait'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status

看起来它没有链接到操作系统中的一些基本库?

详细说明:

gcc --version --> 5.4.0 20160609
ldd --version --> 2.23
OS            --> Ubuntu 16.04 x64, 4.8.0-36-generic kernel

2 个答案:

答案 0 :(得分:2)

您似乎错过了pthread库的链接。将-pthread添加到编译命令:

g++ -o main main.o -pthread -lwdt_dio

检查sem_wait(3)的手册页显示:

  

与-pthread链接。

注意:sem_wait是随机选择的,所有这些都应该指定

答案 1 :(得分:1)

如果您仔细阅读错误,则表示未定义来自libwdt_dio.so。它还说明了哪个函数有一个未定义的引用。

在这种特殊情况下,它是pthread库。它可能应该被提及为WDT lib文档中的依赖项。

如果您正在通过命令行进行编译,则可以将-pthread添加到g ++命令中。