使用boost::chrono::system_clock获取时间会导致链接器错误



我有程序链接到系统,时钟和线程在boost。但是简单地添加auto now = boost::chrono::system_clock::now()会给我一个链接错误。

下面是我的main.cpp

#include <iostream>
#include <boost/chrono.hpp>
#include <boost/circular_buffer.hpp>
int main(int argc, char* argv[]) {
boost::circular_buffer<int> test_ring;
test_ring.resize(10);
std::cout << "Boost ring size is" << test_ring.size() << std::endl;

// Below line of code causes linker error while I am well able to use boost componenets as you can see above
auto now = boost::chrono::system_clock::now();
return 0;
}
下面是我的CMakelists.txt
cmake_minimum_required(VERSION 3.4)
project(TestProgram)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
find_package(Boost REQUIRED COMPONENTS system chrono thread)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(TestProgram TestProgram.cpp)

我得到以下链接器错误:

Undefined symbols for architecture x86_64:
"boost::chrono::system_clock::now()", referenced from:
_main in TestProgram.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [TestProgram] Error 1
make[1]: *** [CMakeFiles/TestProgram.dir/all] Error 2
make: *** [all] Error 2

问题:
我做错了什么?我是否缺少任何boost组件链接?正如您所看到的,我能够链接到其他Boost组件,如我的机器上的boost/circular_buffer,只是boost::chrono::system_clock不起作用。

环境:
我在macOS Catalina 10.15.7上使用以下clang编译器。

Apple clang version 12.0.0 (clang-1200.0.32.21)
Target: x86_64-apple-darwin19.6.0
Thread model: posix

add_executable后加target_link_libraries:

add_executable(TestProgram TestProgram.cpp)
target_link_libraries(TestProgram ${Boost_LIBRARIES})

最新更新