0
代碼編譯和鏈接在Windows下很好,但是,在Ubuntu上,當使用boost::timer::auto_cpu_timer timer(short, string);
構造函數時,鏈接失敗。奇怪的升壓auto_cpu_timer undefined引用錯誤
另外,在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()
你是對的!你解決了我的問題!非常感謝!!! – grayfox