我正在尝试编译并链接 Novell LDAP C SDK 中的示例文件,但由于
pthread
库中存在 3 个未解析的符号,链接失败。我在 Ubuntu 13.10 (Saucy) 上使用 gcc 4.8.1。我已正确地将 pthread
库作为命令行上的最后一个库包含在内,但这些特定符号仍未得到解析。
$ gcc -Wl,-trace-symbol=pthread_mutexattr_settype -Wl,-trace-symbol=pthread_mutexattr_init -Wl,-trace-symbol=pthread_mutex_trylock -o search search.o -L../lib -lldapsdk -lpthread
../lib/libldapsdk.so: reference to pthread_mutexattr_settype<br>
../lib/libldapsdk.so: reference to pthread_mutexattr_init<br>
../lib/libldapsdk.so: reference to pthread_mutex_trylock<br>
/lib/i386-linux-gnu/libpthread.so.0: definition of pthread_mutexattr_settype<br>
/lib/i386-linux-gnu/libpthread.so.0: definition of pthread_mutexattr_init<br>
/lib/i386-linux-gnu/libpthread.so.0: definition of pthread_mutex_trylock<br>
../lib/libldapsdk.so: undefined reference to pthread_mutexattr_settype<br>
../lib/libldapsdk.so: undefined reference to pthread_mutex_trylock<br>
../lib/libldapsdk.so: undefined reference to pthread_mutexattr_init<br>
collect2: error: ld returned 1 exit status
$ readelf --all /lib/i386-linux-gnu/libpthread.so.0|grep pthread_mutexattr_settype<br>
114: 00009d90 36 FUNC GLOBAL DEFAULT 13 pthread_mutexattr_settype@@GLIBC_2.1<br>
493: 00009d90 36 FUNC GLOBAL DEFAULT 13 pthread_mutexattr_settype
在cmake中你可以添加
set( CMAKE_CXX_FLAGS " -pthread " )
请勿编辑间距!
如果使用 CMake,请勿使用
CMAKE_C[XX]_FLAGS
。相反,你应该这样做 target_link_libraries(your_library PRIVATE pthread)
。