简单线程程序的链接器错误(来自 boost_chrono 的符号丢失)

Posted

技术标签:

【中文标题】简单线程程序的链接器错误(来自 boost_chrono 的符号丢失)【英文标题】:Linker error with simple threaded program (symbols from boost_chrono missing) 【发布时间】:2018-03-15 07:36:01 【问题描述】:

我正在学习 boost::timed_mutex

以下代码无法编译:

#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <iostream>

void wait(int seconds)

  boost::this_thread::sleep_for(boost::chrono::secondsseconds);


boost::timed_mutex mutex;

void thread1()

  using boost::this_thread::get_id;
  for (int i = 0; i < 5; ++i)
  
    wait(1);
    boost::unique_lock<boost::timed_mutex> lockmutex;
    std::cout << "Thread " << get_id() << ": " << i << std::endl;
    boost::timed_mutex *m = lock.release();
    m->unlock();
  


void thread2()

  using boost::this_thread::get_id;
  for (int i = 0; i < 5; ++i)
  
    wait(1);
    boost::unique_lock<boost::timed_mutex> lockmutex,
      boost::try_to_lock;
    if (lock.owns_lock() || lock.try_lock_for(boost::chrono::seconds1))
    
      std::cout << "Thread " << get_id() << ": " << i << std::endl;
    
  


int main()

  boost::thread t1thread1;
  boost::thread t2thread2;
  t1.join();
  t2.join();

我的编译命令是:

g++ -std=c++11 unique_lock.cpp -o unique_lock -g -lboost_system -lboost_thread-mt -pthread -lboost_timer

错误类似于以下内容:

/bin/ld: /tmp/ccRVKHNh.o: undefined reference to symbol '_ZN5boost6chrono12system_clock3nowEv'
/usr/lib64/libboost_chrono.so.1.53.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

怎么了?

【问题讨论】:

错误是:/bin/ld: /tmp/ccRVKHNh.o: 未定义对符号 '_ZN5boost6chrono12system_clock3nowEv' /usr/lib64/libboost_chrono.so.1.53.0 的引用:添加符号时出错:DSO 缺失命令行 collect2:错误:ld 返回 1 个退出状态 您尝试添加-lboost_chrono 吗? 我忘了-lboost_chrono,现在可以用了~ 【参考方案1】:

显然 -lboost_chrono 不见了。

Demo

【讨论】:

以上是关于简单线程程序的链接器错误(来自 boost_chrono 的符号丢失)的主要内容,如果未能解决你的问题,请参考以下文章

构建时的链接器符号算术计算错误的结果

使用 mingw-w64 编译 mex 的链接器错误

使用 C++ 的 SOIL 库链接器错误问题

来自boost / serialization / vector #include的链接器错误

GLEW 链接错误 [重复]

RestKit CocoaPods 链接器错误