CMake 使用错误的链接器标志(动态与静态)
CMake using the wrong linker flags (dynamic vs static)
我正在静态编译我的程序,如下所示:
cmake_minimum_required(VERSION 2.8)
project(statictest)
set(LINK_SEARCH_START_STATIC ON)
set(LINK_SEARCH_END_STATIC ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
SET(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
SET(BUILD_SHARED_LIBRARIES OFF)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -static-libgcc -static-libstdc++")
find_package(Boost REQUIRED COMPONENTS filesystem system program_options regex thread chrono)
include_directories(${Boost_INCLUDE_DIRS})
set(LIBS ${Boost_LIBRARIES})
add_executable(test
"src/test/main.cpp"
)
target_link_libraries(test ${LIBS})
linker 行应该包含 BOOST 的静态 linkages,因为我找到了 static 版本的库。
这是 cmake 生成的 linker 行:
/usr/bin/c++ -g -static -static-libgcc -static-libstdc++ CMakeFiles/test.dir/src/test/main.cpp.o -o test -rdynamic -Wl,-Bdynamic /usr/local/lib/libboost_filesystem.a /usr/local/lib/libboost_system.a /usr/local/lib/libboost_program_options.a /usr/local/lib/libboost_regex.a /usr/local/lib/libboost_thread.a /usr/local/lib/libboost_chrono.a
请注意所有以 .a
结尾的 static BOOST 库之前的 -Wl,-bdynamic
。如何使用静态标志强制 CMAKE link 这些?
LINK_SEARCH_END_STATIC 是目标 属性,你必须将它应用到目标本身:
SET_TARGET_PROPERTIES(test PROPERTIES LINK_SEARCH_END_STATIC 1)
我正在静态编译我的程序,如下所示:
cmake_minimum_required(VERSION 2.8)
project(statictest)
set(LINK_SEARCH_START_STATIC ON)
set(LINK_SEARCH_END_STATIC ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
SET(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
SET(BUILD_SHARED_LIBRARIES OFF)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -static-libgcc -static-libstdc++")
find_package(Boost REQUIRED COMPONENTS filesystem system program_options regex thread chrono)
include_directories(${Boost_INCLUDE_DIRS})
set(LIBS ${Boost_LIBRARIES})
add_executable(test
"src/test/main.cpp"
)
target_link_libraries(test ${LIBS})
linker 行应该包含 BOOST 的静态 linkages,因为我找到了 static 版本的库。
这是 cmake 生成的 linker 行:
/usr/bin/c++ -g -static -static-libgcc -static-libstdc++ CMakeFiles/test.dir/src/test/main.cpp.o -o test -rdynamic -Wl,-Bdynamic /usr/local/lib/libboost_filesystem.a /usr/local/lib/libboost_system.a /usr/local/lib/libboost_program_options.a /usr/local/lib/libboost_regex.a /usr/local/lib/libboost_thread.a /usr/local/lib/libboost_chrono.a
请注意所有以 .a
结尾的 static BOOST 库之前的 -Wl,-bdynamic
。如何使用静态标志强制 CMAKE link 这些?
LINK_SEARCH_END_STATIC 是目标 属性,你必须将它应用到目标本身:
SET_TARGET_PROPERTIES(test PROPERTIES LINK_SEARCH_END_STATIC 1)