代码在Windows下编译和链接很好,但是,在Ubuntu上,当使用boost::timer::auto_cpu_timer timer(short, string);
构造函数时,链接失败。
此外,在Windows上我也不需要链接到boost chrono库,但在Ubuntu上它是必需的。
的main.cpp
#include <boost/timer/timer.hpp>
int main()
{
// Link Failed!
//
// main.cpp:(.text.startup+0x96):
// undefined reference to
// `boost::timer::auto_cpu_timer::auto_cpu_timer(
// short,
// std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
// '
boost::timer::auto_cpu_timer timer(3, "Wall time: %w seconds\n");
// Link OK
boost::timer::auto_cpu_timer timer2;
return 0;
}
的CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(auto_cpu_timer_test)
aux_source_directory(. SRC_LIST)
# Setup Boost
if (WIN32)
set(BOOST_LIBRARYDIR C:/boost_1_59_0/stage/lib)
set(BOOST_INCLUDEDIR C:/boost_1_59_0)
else ()
set(BOOST_LIBRARYDIR /home/me/boost_1_59_0/stage/lib)
set(BOOST_INCLUDEDIR /home/me/boost_1_59_0)
endif()
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
link_directories(${LINK_DIRECTORIES} ${BOOST_LIBRARYDIR})
include_directories(${INCLUDE_DIRECTORIES} ${BOOST_INCLUDEDIR})
set( CMAKE_BUILD_TYPE "Release" )
if(WIN32)
set( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd" )
set( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT" )
else()
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
endif()
add_executable(${PROJECT_NAME} ${SRC_LIST})
if (WIN32)
target_link_libraries(${PROJECT_NAME} libboost_system-vc140-mt-s-1_59.lib libboost_timer-vc140-mt-s-1_59.lib)
else()
target_link_libraries(${PROJECT_NAME} libboost_system-mt-s.a libboost_timer-mt-s.a libboost_chrono-mt-s.a)
endif ()
答案 0 :(得分:2)
最可能的原因是你的Boost不是用C ++ 11模式构建的,但你的应用程序是使用显式-std = c ++ 11构建的。
您可以删除“bin.b2”和“stage”目录,然后重试:
b2 ... cxxflags=-std=c++11 linkflags=-std=c++11
这在Windows上不是问题,因为Visual Studio默认启用了C ++ 11。您不需要在Windows上链接chrono的原因可能是由于自动链接 - 您将库库添加到库搜索路径,并且您不禁用自动链接,因此所有必需的库都会自动包含。