簡體   English   中英

使用C ++ Boost線程的架構x86_64的未定義符號

[英]Undefined symbols for architecture x86_64 with C++ Boost 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();
    }

但是,當我嘗試編譯它時,終端將其吐給我:

   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)

我不知道為什么。

如果有幫助,我將庫安裝在

   /usr/local directory

和使用

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

編譯我的程序

它可能只是我剛剛忽略的真正簡單而小的內容(例如我忘記了頭文件),但似乎找不到它。 如果有人有任何見識,那就太好了! 謝謝!

作為快速解決方案,您可以顯式提供所需的Boost組件。 在您的情況下,請在CMakeLists.txt文件中使用以下命令:

FIND_PACKAGE(需要Boost COMPONENTS線程)

嘗試使用: -lboost_thread-mt ,在我的情況下效果很好。

例如:

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