gpt4 book ai didi

c++ - 即使使用 -lboost_thread 也会出现 "undefined reference"错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:22 26 4
gpt4 key购买 nike

我在尝试使用 boost::shared_mutex 时遇到了一些奇怪的编译器/链接器错误。我在运行 32 位 rhel 6.2 的虚拟机上使用 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
}

制作输出:

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 命令中添加一些额外的东西吗?我可以在 /usr/local/lib 中看到 libboost_thread.a,所以我认为这意味着线程库已正确构建。

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

编辑: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()'

最佳答案

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

@jww 建议我展示实际的编译和链接命令调用,而不是 CMake 的输出,我已将其添加到我的问题中。如您所见,该命令链接了 boost_systempthread,但没有链接 boost_thread

深入研究我的项目后,我意识到我已将 boost_thread 添加到我的发布版本中,但没有添加到我试图编译的单元测试版本中。将 boost_thread 添加到单元测试的 CMakeLists.txt 会立即消除错误。

关于c++ - 即使使用 -lboost_thread 也会出现 "undefined reference"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44508173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com