Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Threaded programming in C++ with the Boost Libraries
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Katherine1
n00b
n00b


Joined: 06 Oct 2010
Posts: 26

PostPosted: Wed Oct 06, 2010 3:13 pm    Post subject: Threaded programming in C++ with the Boost Libraries Reply with quote

I am trying to figure out threading with the Boost libraries. I currently have some code that, from what I've seen in the resources I've been looking at, should work. IT doesn't compile, however. Here is my code:

Code:
#include <boost/thread.hpp>
#include <stdio.h>

void testThread() {
    printf("Hello World");
}

int main(int argc, char *argv[]) {
   
    boost::thread threadTest(testThread);
    threadTest.join();
   
    return 0;
}


Here is the compiler output for this code:

$ g++ test.cpp -o test
Code:
/tmp/ccfY35Np.o: In function `main':
test.cpp:(.text+0x45): undefined reference to `boost::thread::join()'
test.cpp:(.text+0x56): undefined reference to `boost::thread::~thread()'
test.cpp:(.text+0x72): undefined reference to `boost::thread::~thread()'
/tmp/ccfY35Np.o: In function `boost::detail::thread_data_base::thread_data_base()':
test.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[boost::detail::thread_data_base::thread_data_base()]+0x26): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/ccfY35Np.o: In function `boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)':
test.cpp:(.text._ZN5boost6threadC1IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thread_move_tIS4_EEEEPNS0_5dummyEE4typeE[boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)]+0x42): undefined reference to `boost::thread::start_thread()'
/tmp/ccfY35Np.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
test.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED1Ev[boost::detail::thread_data<void (*)()>::~thread_data()]+0x1f): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/ccfY35Np.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
test.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED0Ev[boost::detail::thread_data<void (*)()>::~thread_data()]+0x1f): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/ccfY35Np.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[typeinfo for boost::detail::thread_data<void (*)()>]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: ld returned 1 exit status


From what I can guess, it looks like I'm missing a header file, despite the fact that boost/thread.hpp should be the only header file I need to use the boost threads.
Back to top
View user's profile Send private message
dmitchell
Veteran
Veteran


Joined: 17 May 2003
Posts: 1159
Location: Austin, Texas

PostPosted: Wed Oct 06, 2010 3:42 pm    Post subject: Reply with quote

This is a linker error. The linker can't find the functions declared in the headers (thread::join() and so on). You need to link the boost threading library. Something like this:

Code:
g++ test.cpp -o test -lboost_thread-mt

That might not be right library name (boost_thread-mt), so if it doesn't work just substitute the proper name.
_________________
Your argument is invalid.
Back to top
View user's profile Send private message
Katherine1
n00b
n00b


Joined: 06 Oct 2010
Posts: 26

PostPosted: Wed Oct 06, 2010 4:59 pm    Post subject: Reply with quote

I see. That worked. Thank you :3
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum