简体   繁体   English

使用C ++ Boost线程的架构x86_64的未定义符号

[英]Undefined symbols for architecture x86_64 with C++ Boost threads

I am new to the Boost C++ library and I was trying to run this simple program that utilizes threads 我是Boost C ++库的新手,我试图运行这个利用线程的简单程序

    #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! 谢谢!

As a quick fix, you could explicitly provide the Boost components that are required. 作为快速解决方案,您可以显式提供所需的Boost组件。 In your case, use the following in your CMakeLists.txt file: 在您的情况下,请在CMakeLists.txt文件中使用以下命令:

FIND_PACKAGE( Boost COMPONENTS thread REQUIRED ) FIND_PACKAGE(需要Boost COMPONENTS线程)

Try using: -lboost_thread-mt , works fine in my case. 尝试使用: -lboost_thread-mt ,在我的情况下效果很好。

For ex: 例如:

g++ -O3 -stdlib=libc++ -std=c++11 -lboost_thread-mt < File-name >.cpp -o < Binary-Name >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM