提升"未定义参考"即使使用-lboost_thread也会出错

时间:2017-06-12 20:27:42

标签: c++ linux boost

尝试使用boost :: shared_mutex时,我遇到了一些奇怪的编译器/链接器错误。我在运行32位rhel 6.2的VM上使用boost v1.61。

导致错误的代码:

hpp文件:

#include <boost/thread/shared_mutex.hpp>

class SharedData
{
public:
  SharedData();
  ~SharedData();

  void packMessage(std::shared_ptr<Message> s);

private:
  // mutex that allows multiple read, single write protection
  boost::shared_mutex m_sharedMutex;
};

cpp文件:

void SharedData::packMessage(std::shared_ptr<Message> s)
{
  // get shared read access
  boost::shared_lock<boost::shared_mutex> lock(m_sharedMutex); // <- this line causes the errors

  // read stuff here
}

make输出:

CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::detail::interruption_checker::interruption_checker(pthread_mutex_t*, pthread_cond_t*)':
/usr/local/include/boost/thread/pthread/thread_data.hpp:195: undefined reference to `boost::detail::get_current_thread_data()'
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::condition_variable::wait(boost::unique_lock<boost::mutex>&)':
/usr/local/include/boost/thread/pthread/condition_variable.hpp:81: undefined reference to `boost::this_thread::interruption_point()'
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::shared_mutex::lock_shared()':
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:186: undefined reference to `boost::this_thread::disable_interruption::disable_interruption()'
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:193: undefined reference to `boost::this_thread::disable_interruption::~disable_interruption()'
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:193: undefined reference to `boost::this_thread::disable_interruption::~disable_interruption()'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/tester] Error 1

我搜索过的所有地方都说这些错误意味着我需要链接boost_thread库,我已经在我的cmake文件中完成了(boost_system包含在同一项目中的其他代码中):

target_link_libraries(${BINARY_NAME} boost_thread boost_system)

但错误仍然存​​在。

最初我使用的是已安装在我机器上的boost 1.41库。当我遇到这些错误时,我运行了yum remove boost-devel,然后手动安装了boost 1.61以查看是否可以纠正错误。我需要添加一些额外的./bootstrap.sh./b2 install命令吗?我可以在libboost_thread.a中看到/usr/local/lib,所以我认为这意味着线程库已正确构建。

有关导致这些错误的原因的任何想法?谢谢!

编辑: Cmake的日志没有我能看到的任何有用的东西,但是make VERBOSE=1给出了以下输出:

cd /home/craig/dev/myProject/build/test && /usr/bin/cmake -E cmake_link_script CMakeFiles/tester.dir/link.txt --verbose=1
/opt/rh/devtoolset-2/root/usr/bin/c++   -std=c++11 -ggdb -Wall -Werror -fprofile-arcs -ftest-coverage -fPIC -O0 -pedantic    -Wl,--export-dynamic CMakeFiles/tester.dir/utilities/googletest/googletest/src/gtest-all.cc.o CMakeFiles/tester.dir/tester.cpp.o CMakeFiles/tester.dir/SharedData/testSharedData.cpp.o CMakeFiles/tester.dir/Common/testFifo.cpp.o CMakeFiles/tester.dir/Common/testCsu.cpp.o CMakeFiles/tester.dir/Messages/testMessage.cpp.o CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o CMakeFiles/tester.dir/__/src/Common/Fifo.cpp.o CMakeFiles/tester.dir/__/src/Common/Csu.cpp.o  -o ../bin/tester -rdynamic -lboost_system -lpthread 
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::detail::interruption_checker::interruption_checker(pthread_mutex_t*, pthread_cond_t*)':
/usr/local/include/boost/thread/pthread/thread_data.hpp:195: undefined reference to `boost::detail::get_current_thread_data()'
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::condition_variable::wait(boost::unique_lock<boost::mutex>&)':
/usr/local/include/boost/thread/pthread/condition_variable.hpp:81: undefined reference to `boost::this_thread::interruption_point()'
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::shared_mutex::lock_shared()':
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:186: undefined reference to `boost::this_thread::disable_interruption::disable_interruption()'
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:193: undefined reference to `boost::this_thread::disable_interruption::~disable_interruption()'
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:193: undefined reference to `boost::this_thread::disable_interruption::~disable_interruption()'

1 个答案:

答案 0 :(得分:0)

原来我的问题就像其他人一样。

@jww建议我显示实际的编译和链接命令调用,而不是CMake的输出,我已经在我的问题中添加了。如您所见,该命令正在关联boost_systempthread,但不关联boost_thread

深入到我的项目中,我意识到我已经将boost_thread添加到我的发布版本中,但没有添加到我的单元测试版本中,这是我尝试编译的内容。将boost_thread添加到单元测试的CMakeLists.txt会立即删除错误。