防止"未定义引用..."提升错误,我需要在编译器标志的最后添加boost库。因此,在CMakeLists.txt中我设置:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pedantic -O0 -Wall -DBOOST_SYSTEM_NO_DEPRECATED -lboost_system -lboost_filesystem")
然而,cmake的详细输出告诉我在我定义的后面附加了额外的标志:
g++ -std=c++11 -pedantic -O0 -Wall -DBOOST_SYSTEM_NO_DEPRECATED -lboost_system -lboost_filesystem CMakeFiles/My_Project.dir/main.cpp.o -o My_Project -L/usr/local/boost_1_60_0/lib
是否可以更改订单?
完整的CMakeLists.txt:
cmake_minimum_required(VERSION 3.4)
project(My_Project)
set(CMAKE_VERBOSE_MAKEFILE ON)
# This is bad but I currently don't have another working solution.
set(BOOSTROOT "/usr/local/boost_1_60_0/")
set(BOOST_ROOT "/usr/local/boost_1_60_0/")
find_package(Boost 1.60.0 COMPONENTS system filesystem REQUIRED)
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
link_directories(${Boost_LIBRARY_DIRS})
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(BoostTest main.cpp)
if(Boost_FOUND)
target_link_libraries(BoostTest ${Boost_LIBRARIES})
endif()
# Boost libraries appended at the end. However, cmake generates flags like this:
# c++ -std=c++11 -pedantic -O0 -Wall -DBOOST_SYSTEM_NO_DEPRECATED -lboost_system -lboost_filesystem CMakeFiles/My_Project.dir/main.cpp.o -o My_Project -L/usr/local/boost_1_60_0/lib
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pedantic -O0 -Wall -DBOOST_SYSTEM_NO_DEPRECATED -lboost_system -lboost_filesystem")
set(SOURCE_FILES main.cpp)
add_executable(My_Project ${SOURCE_FILES})
由于
答案 0 :(得分:1)
您应该使用target_link_libraries而不是手动将-lboost
指令附加到编译器标志中。
TARGET_LINK_LIBRARIES(My_Project boost)
还应该提到的是,在编译目标文件后,可能会将链接器作为单独的调用调用