
Hi all, I've started just recently to use boost in my C++ application to manage some threads. However, when I link my application I get strange undefined references g++ -g -O -o test_exe -lcppunit -ldl -lpthread -lboost_thread Main.o test.o test.o(.text+0x874): In function `threadTestCases::spawnThread()': : undefined reference to `boost::thread::thread(boost::function0<void, std::allocator<boost::function_base> > const&)' test.o(.text+0x8b7): In function `threadTestCases::spawnThread()': : undefined reference to `boost::thread::join()' test.o(.text+0x8ce): In function `threadTestCases::spawnThread()': : undefined reference to `boost::thread::~thread()' test.o(.text+0x8eb): In function `threadTestCases::spawnThread()': : undefined reference to `boost::thread::~thread()' collect2: ld returned 1 exit status make: *** [test_exe] Error 1 As you can see I have linked in the boost_thread and pthread but still I get the problem. My LD_LIBRARY_PATH is correctly setup to define the path where boost is installed (/usr/lib/) The code is as follows and is used in a CPPUNIT framework. The boost code is in the spawnThread member function. //--------------------------------------------------------------------- #include "test.h" #include <ostream> #include <pthread.h> #include <boost/thread/thread.hpp> #include <boost/bind.hpp> using namespace std; using namespace boost; CPPUNIT_TEST_SUITE_REGISTRATION( threadTestCases ); void threadTestCases::setUp() { cout << "\nthreadTestCases::setUp\n" << endl ; } void threadTestCases::tearDown() { cout << "\nthreadTestCases::tearDown\n" << endl ; } void threadTestCases::testThread() { cout << "Thread [testThread] spawned\n" << endl; try { CPPUNIT_ASSERT(1 == 2); cout << "Thread end" << endl; } catch(...) { cout << "Exception caught in testThread, continue\n" << endl; } pthread_exit(0); } void *my_thread(void *) { cout << "Thread [my_thread] spawned\n" << endl ; try { CPPUNIT_ASSERT(1 == 2); } catch(...) { cout << "Exception caught in my_thread, continue\n" << endl; } pthread_exit(0); } void threadTestCases::spawnThread() { pthread_t thread[2]; int ret; cout << "\nSpawning new thread\n" << endl; // Create thread via boost boost::thread mfThread(boost::bind(&threadTestCases::testThread, this)); // Start execution of thread mfThread.join(); } Is there anything I have done wrong in the code or perhaps in the build environment which is using Boost library v1.32.0-1 for Linux RedHat with g++ (GCC 3.4.6) Any help would be appreciated as this has been bugging me for several hours today. Cheers Carl

You need to specify where boost headers are located, -I/path-to-boost-headers. -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Carl Wainwright Sent: Tuesday, May 08, 2007 11:56 AM To: boost-users@lists.boost.org Subject: [Boost-users] Undefined reference Hi all, I've started just recently to use boost in my C++ application to manage some threads. However, when I link my application I get strange undefined references g++ -g -O -o test_exe -lcppunit -ldl -lpthread -lboost_thread Main.o test.o test.o(.text+0x874): In function `threadTestCases::spawnThread()': : undefined reference to `boost::thread::thread(boost::function0<void, std::allocator<boost::function_base> > const&)' test.o(.text+0x8b7): In function `threadTestCases::spawnThread()': : undefined reference to `boost::thread::join()' test.o(.text+0x8ce): In function `threadTestCases::spawnThread()': : undefined reference to `boost::thread::~thread()' test.o(.text+0x8eb): In function `threadTestCases::spawnThread()': : undefined reference to `boost::thread::~thread()' collect2: ld returned 1 exit status make: *** [test_exe] Error 1 As you can see I have linked in the boost_thread and pthread but still I get the problem. My LD_LIBRARY_PATH is correctly setup to define the path where boost is installed (/usr/lib/) The code is as follows and is used in a CPPUNIT framework. The boost code is in the spawnThread member function. //--------------------------------------------------------------------- #include "test.h" #include <ostream> #include <pthread.h> #include <boost/thread/thread.hpp> #include <boost/bind.hpp> using namespace std; using namespace boost; CPPUNIT_TEST_SUITE_REGISTRATION( threadTestCases ); void threadTestCases::setUp() { cout << "\nthreadTestCases::setUp\n" << endl ; } void threadTestCases::tearDown() { cout << "\nthreadTestCases::tearDown\n" << endl ; } void threadTestCases::testThread() { cout << "Thread [testThread] spawned\n" << endl; try { CPPUNIT_ASSERT(1 == 2); cout << "Thread end" << endl; } catch(...) { cout << "Exception caught in testThread, continue\n" << endl; } pthread_exit(0); } void *my_thread(void *) { cout << "Thread [my_thread] spawned\n" << endl ; try { CPPUNIT_ASSERT(1 == 2); } catch(...) { cout << "Exception caught in my_thread, continue\n" << endl; } pthread_exit(0); } void threadTestCases::spawnThread() { pthread_t thread[2]; int ret; cout << "\nSpawning new thread\n" << endl; // Create thread via boost boost::thread mfThread(boost::bind(&threadTestCases::testThread, this)); // Start execution of thread mfThread.join(); } Is there anything I have done wrong in the code or perhaps in the build environment which is using Boost library v1.32.0-1 for Linux RedHat with g++ (GCC 3.4.6) Any help would be appreciated as this has been bugging me for several hours today. Cheers Carl _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Carl Wainwright wrote:
Hi all,
I've started just recently to use boost in my C++ application to manage some threads. However, when I link my application I get strange undefined references
g++ -g -O -o test_exe -lcppunit -ldl -lpthread -lboost_thread Main.o test.o test.o(.text+0x874): In function `threadTestCases::spawnThread()': : undefined reference to `boost::thread::thread(boost::function0<void, std::allocator<boost::function_base> > const&)' test.o(.text+0x8b7): In function `threadTestCases::spawnThread()': : undefined reference to `boost::thread::join()' test.o(.text+0x8ce): In function `threadTestCases::spawnThread()': : undefined reference to `boost::thread::~thread()' test.o(.text+0x8eb): In function `threadTestCases::spawnThread()': : undefined reference to `boost::thread::~thread()' collect2: ld returned 1 exit status make: *** [test_exe] Error 1
As you can see I have linked in the boost_thread and pthread but still I get the problem. My LD_LIBRARY_PATH is correctly setup to define the path where boost is installed (/usr/lib/)
Hi Carl, Try changing the order that you specify the arguments to g++. You'll want to put Main.o and test.o before the request to link in the libraries. Here is a link that has some more information (it is about DJGPP, but should hold true for your version of g++): http://www.delorie.com/djgpp/v2faq/faq8_10.html David
participants (3)
-
Carl Wainwright
-
David Walthall
-
Xu, Peng