“架构x86_64的未定义符号:” Boost 1.73.0 Clion MacOS

问题描述

我是C ++的超级新手,刚开始涉足Boost C ++库

我目前在MacOS Catalina上,并使用CLion作为我的c ++ IDE。

我使用自制软件brew install boost

安装了boost

我正在关注以下的一些教程:http://antonym.org/2009/05/threading-with-boost-part-i-creating-threads.html了解boost::thread

代码如下:

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


void workerFunc()
{
    boost::posix_time::seconds workTime(3);
    std::cout << "Worker: running" << std::endl;
    // Pretend to do something useful...
    boost::this_thread::sleep(workTime);
    std::cout << "Worker: finished" << std::endl;
}


int main(int argc,char* argv[])
{
    std::cout << "main: startup" << std::endl;
    boost::thread workerThread(workerFunc);
    std::cout << "main: waiting for thread" << std::endl;
    workerThread.join();
    std::cout << "main: done" << std::endl;
    return 0;
}

这是我的CMakeLists.txt文件

cmake_minimum_required(VERSION 3.17)
project(cpp_boost)

set(CMAKE_CXX_STANDARD 14)

find_package(Boost 1.73.0 COMPONENTS system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})

最后,我在运行代码时遇到的构建错误。

====================[ Build | cpp_boost | Debug ]===============================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/parvpatel/Desktop/development/dev-projects/lang-tutorials/cpp-tutorials/cpp-boost/cmake-build-debug --target cpp_boost -- -j 12
[ 50%] Building CXX object CMakeFiles/cpp_boost.dir/main.cpp.o
[100%] Linking CXX executable cpp_boost
Undefined symbols for architecture x86_64:
  "boost::this_thread::interruption_point()",referenced from:
      boost::condition_variable::wait(boost::unique_lock<boost::mutex>&) in main.cpp.o
      boost::condition_variable::do_wait_until(boost::unique_lock<boost::mutex>&,boost::detail::real_platform_timepoint const&) in main.cpp.o
  "boost::detail::thread_data_base::~thread_data_base()",referenced from:
      boost::detail::thread_data<void (*)()>::~thread_data() in main.cpp.o
  "boost::detail::get_current_thread_data()",referenced from:
      boost::detail::interruption_checker::interruption_checker(_opaque_pthread_mutex_t*,_opaque_pthread_cond_t*) in main.cpp.o
  "boost::thread::join_noexcept()",referenced from:
      boost::thread::join() in main.cpp.o
  "boost::thread::native_handle()",referenced from:
      boost::thread::get_id() const in main.cpp.o
  "boost::thread::start_thread_noexcept()",referenced from:
      boost::thread::start_thread() in main.cpp.o
  "boost::thread::detach()",referenced from:
      boost::thread::~thread() in main.cpp.o
  "typeinfo for boost::detail::thread_data_base",referenced from:
      typeinfo for boost::detail::thread_data<void (*)()> in main.cpp.o
  "vtable for boost::detail::thread_data_base",referenced from:
      boost::detail::thread_data_base::thread_data_base() in main.cpp.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)
make[3]: *** [cpp_boost] Error 1
make[2]: *** [CMakeFiles/cpp_boost.dir/all] Error 2
make[1]: *** [CMakeFiles/cpp_boost.dir/rule] Error 2
make: *** [cpp_boost] Error 2

我试图浏览互联网,但找不到任何具体内容。我发现它对target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})很有用,但是将其添加到CMakeLists.txt文件后,错误仍然存​​在。

任何帮助将不胜感激。

谢谢:)

解决方法

谢谢一些程序员花花公子

它只是向find_package添加thread使其起作用

这是更新的CMakeLists.txt

cmake_minimum_required(VERSION 3.17)
project(cpp_boost)

set(CMAKE_CXX_STANDARD 14)

find_package(Boost 1.73.0 COMPONENTS system filesystem thread REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...