3

I was trying to run a small test program on Xcode (4.2) using C++ after encountering error in my project.

#include <boost/thread.hpp>
#include <boost/bind.hpp>    

int main (int argc, const char * argv[])
{
    boost::thread_group tg;
    return 0;
}

But the whole program fails to build, outputting error:

Undefined symbols for architecture x86_64:
  "boost::thread::~thread()", referenced from:
      boost::thread_group::~thread_group()in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Then I tried using

thread_group * tg = new thread_group();

which compiles without issue, up until I tried to invoke

tg->join_all();

where the compiler outputs errors such as:

Undefined symbols for architecture x86_64:
  "boost::detail::get_current_thread_data()", referenced from:
      boost::detail::interruption_checker::interruption_checker(_opaque_pthread_mutex_t*, _opaque_pthread_cond_t*)in main.o
  "boost::this_thread::interruption_point()", referenced from:
      boost::condition_variable::wait(boost::unique_lock<boost::mutex>&) in main.o
  "boost::this_thread::disable_interruption::disable_interruption()", referenced from:
      boost::shared_mutex::lock_shared()      in main.o
  "boost::this_thread::disable_interruption::~disable_interruption()", referenced from:
      boost::shared_mutex::lock_shared()      in main.o
  "boost::thread::join()", referenced from:
      boost::thread_group::join_all()     in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Does anyone know how to resolve these issues ? I've been using other functions such as BOOST_FOREACH without any issues. But encounter these while trying to use threads.

Do I need to:

  • Specify flags on my 'Other Linker Flags'?
  • reinstall boost or some sort ? Currently I am using Boost 1.49.0, installed using homebrew (i.e. sudo brew install boost)

Or are there any other specific configurations that I need to to include ?

sub_o
  • 2,642
  • 5
  • 28
  • 41
  • `boost_thread` requires explicit linking. As for `BOOST_FOREACH`, that's a macro, and is expanded by the pre-processor. – chrisaycock Jul 23 '12 at 17:04
  • @chrisaycock Well, even though I've added -lboost_thread, it still gives out the same errors. – sub_o Jul 23 '12 at 17:14

1 Answers1

4

The boost thread library has an actual library object you need to link against (-lboost_thread or sometimes -lboost_thread-mt).

jww
  • 97,681
  • 90
  • 411
  • 885
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • I've added -lboost_thread under my 'Other linker flags', but it still outputs the same errors. – sub_o Jul 23 '12 at 17:15
  • @JohnSmith And you are sure the linker can find the library? You might need to set the library search paths as well... – trojanfoe Jul 23 '12 at 17:17
  • Yeah, I set the include path and the library search path correctly. But still the same error. I read elsewhere about reinstalling it with --universal flag, is that the cause ? – sub_o Jul 23 '12 at 17:20
  • @JohnSmith You get a different error message when an architecture is not available in a library. – trojanfoe Jul 23 '12 at 17:24
  • You're right. I forgot to add in all the libraries into the 'Link Binary with Libraries' section. – sub_o Jul 23 '12 at 17:30
  • For OS X and Brew, it looks like the [multithreaded version of Boost thread is `-lboost_thread-mt`](http://stackoverflow.com/a/40033560/608639) – jww Oct 14 '16 at 01:49