更改编译器标志的顺序
Change the order of compiler flags
为了防止 "undefined reference to..." boost 错误,我需要在编译器标志的最后附加 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})
谢谢
您应该使用 target_link_libraries 而不是手动将 -lboost
指令附加到您的编译器标志中。
TARGET_LINK_LIBRARIES(My_Project boost)
还应该提到的是,链接器可能作为单独的调用被调用在编译目标文件
之后
为了防止 "undefined reference to..." boost 错误,我需要在编译器标志的最后附加 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})
谢谢
您应该使用 target_link_libraries 而不是手动将 -lboost
指令附加到您的编译器标志中。
TARGET_LINK_LIBRARIES(My_Project boost)
还应该提到的是,链接器可能作为单独的调用被调用在编译目标文件
之后