构建可执行文件时找不到pthread库?

时间:2011-01-17 07:17:33

标签: c++ makefile pthreads

我正在尝试为我创建的ThreadManager类构建一个简单的测试程序。正如您在下面的输出中看到的,我的ThreadManager和主代码似乎很好地编译成目标文件,但是当创建可执行文件时,它似乎无法找到对pthread类中任何函数的正确引用。我错过了什么?

g++    -c -o ThreadManager.o ThreadManager.cpp  
g++    -c -o main.o main.cpp  
g++ -o tm_test ThreadManager.o main.o -g    -lm  

ThreadManager.o: In function `ThreadManager::Create(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, void* (*)(void*))':  
ThreadManager.cpp:(.text+0x276): undefined reference to `pthread_create'  
ThreadManager.o: In function `ThreadManager::Create(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, void* (*)(void*), void*)':  
ThreadManager.cpp:(.text+0x4a3): undefined reference to `pthread_create'  
ThreadManager.o: In function `ThreadManager::Create(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, void* (*)(void*), void*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':  
ThreadManager.cpp:(.text+0x70b): undefined reference to `pthread_create'  
ThreadManager.o: In function `ThreadManager::Cleanup(int)':  
ThreadManager.cpp:(.text+0x8d4): undefined reference to `pthread_join'  
ThreadManager.cpp:(.text+0xa5f): undefined reference to `pthread_join'  
ThreadManager.cpp:(.text+0xbe1): undefined reference to `pthread_kill'  
ThreadManager.o: In function `ThreadManager::Resurrect()':  
ThreadManager.cpp:(.text+0xe8c): undefined reference to `pthread_create'  
ThreadManager.o: In function `ThreadManager::Kill(int)':  
ThreadManager.cpp:(.text+0x125f): undefined reference to `pthread_cancel'  
ThreadManager.cpp:(.text+0x1285): undefined reference to `pthread_join'  
ThreadManager.o:(.rodata+0x1a0): undefined reference to `pthread_cancel'  
main.o:(.rodata+0x5c): undefined reference to `pthread_cancel'  
collect2: ld returned 1 exit status  
make: *** [tm_test] Error 1

谢谢,

1 个答案:

答案 0 :(得分:7)

链接到pthread库;

g++ -c -o ThreadManager.o ThreadManager.cpp 
g++ -c -o main.o main.cpp 
g++ -o tm_test -lm -lpthread