2

I am new to the Boost C++ library and I was trying to run this simple program that utilizes threads

    #include <boost/thread.hpp>
    #include <iostream>
    using namespace std;

    void wait(int seconds)
    {
        boost::this_thread::sleep(boost::posix_time::seconds(seconds));
    }

    void threadAction()
    {
        int i;
        for(i=0;i<5;i++)
        {
            wait(1);
            cout << i << endl;
        }
    }

    int main()
    {
        boost::thread myThread(threadAction);
        myThread.join();
    }

However, when I tried to compile it, terminal spit this back at me:

   Undefined symbols for architecture x86_64:
      "boost::this_thread::hiden::sleep_until(timespec const&)", referenced from:
          boost::this_thread::sleep(boost::posix_time::ptime const&) in simpleThreadExample-b4b0cd.o
      "boost::detail::thread_data_base::~thread_data_base()", referenced from:
          boost::detail::thread_data<void (*)()>::~thread_data() in simpleThreadExample-b4b0cd.o
      "boost::system::system_category()", referenced from:
          ___cxx_global_var_init2 in simpleThreadExample-b4b0cd.o
          boost::thread_exception::thread_exception(int, char const*) in simpleThreadExample-b4b0cd.o
      "boost::system::generic_category()", referenced from:
          ___cxx_global_var_init in simpleThreadExample-b4b0cd.o
          ___cxx_global_var_init1 in simpleThreadExample-b4b0cd.o
      "boost::thread::join_noexcept()", referenced from:
          boost::thread::join() in simpleThreadExample-b4b0cd.o
      "boost::thread::native_handle()", referenced from:
          boost::thread::get_id() const in simpleThreadExample-b4b0cd.o
      "boost::thread::start_thread_noexcept()", referenced from:
          boost::thread::start_thread() in simpleThreadExample-b4b0cd.o
      "boost::thread::detach()", referenced from:
          boost::thread::~thread() in simpleThreadExample-b4b0cd.o
      "typeinfo for boost::detail::thread_data_base", referenced from:
          typeinfo for boost::detail::thread_data<void (*)()> in simpleThreadExample-b4b0cd.o
      "vtable for boost::detail::thread_data_base", referenced from:
          boost::detail::thread_data_base::thread_data_base() in simpleThreadExample-b4b0cd.o
      NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

and I'm not sure why.

If it helps, I installed the library in my

   /usr/local directory

and use

  g++ -std=c++11 -I /usr/local/boost_1_57_0 main.cpp

to compile my program

It's probably something really simple and small (like I forgot a header file) that I just overlooked, but I can't seem to find it. If anyone has any insight, that would be brilliant! Thanks!

busebd12
  • 997
  • 2
  • 12
  • 24
  • see http://stackoverflow.com/q/26952361/89339 – bn. Mar 06 '15 at 14:45
  • You forgot to link with the boost library. – eerorika Mar 06 '15 at 14:58
  • @user2079303: what do you mean? I thought doing adding the flag: -I /usr/local/boost_1_57_0 did link with the boost library... – busebd12 Mar 06 '15 at 18:55
  • @xXAnointedXx I mean that you must link with any separately compiled library that you use. `-I dir` is not an option for the linker. It will `Add the directory dir to the list of directories to be searched for header files.` as you can see from the [gcc manual](https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/Preprocessor-Options.html#Preprocessor-Options) – eerorika Mar 06 '15 at 20:12
  • So, if this is the case, how would I like the directory separately? – busebd12 Mar 06 '15 at 22:37

2 Answers2

1

Try using: -lboost_thread-mt, works fine in my case.

For ex:

g++ -O3 -stdlib=libc++ -std=c++11 -lboost_thread-mt < File-name >.cpp -o < Binary-Name >
spenibus
  • 4,339
  • 11
  • 26
  • 35
aalekhN
  • 37
  • 3
0

As a quick fix, you could explicitly provide the Boost components that are required. In your case, use the following in your CMakeLists.txt file:

FIND_PACKAGE( Boost COMPONENTS thread REQUIRED )