使用 boost::chrono::system_clock 获取时间会导致链接器错误
Using boost::chrono::system_clock to get time causes linker error
我有一个程序正在 linking 到 boost 中的系统、计时和线程。但是简单地添加 auto now = boost::chrono::system_clock::now()
会给我一个 linker 错误。
以下是我的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)
我得到以下 linker error:
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
问题:
我究竟做错了什么?我是否缺少 link 的任何增强组件?如您所见,我可以在我的机器上 link 到其他 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})
我有一个程序正在 linking 到 boost 中的系统、计时和线程。但是简单地添加 auto now = boost::chrono::system_clock::now()
会给我一个 linker 错误。
以下是我的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)
我得到以下 linker error:
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
问题:
我究竟做错了什么?我是否缺少 link 的任何增强组件?如您所见,我可以在我的机器上 link 到其他 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})