Kaigi
Kaigi

Reputation: 325

pthread library not found when building executable?

I'm trying to build a simple test program for a ThreadManager class I created. As you can see in the output below, my ThreadManager and main code seems to compile into object files just fine, but then when creating the executable, it can't seem to find proper references to any of the functions from the pthread class. What am I missing?

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

Thanks,

Upvotes: 2

Views: 9548

Answers (1)

ismail
ismail

Reputation: 47572

Link to the pthread library;

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

Upvotes: 7

Related Questions