我正在静态编译我的程序,如下所示:
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})
链接器行应包含BOOST的静态链接,因为我找到了库的静态版本。
这是cmake生成的链接器行:
/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
请注意以-Wl,-bdynamic
结尾的所有静态 BOOST库之前的.a
。如何强制CMAKE将这些与静态标志链接?
答案 0 :(得分:2)
LINK_SEARCH_END_STATIC是目标属性,您必须将其应用于目标本身:
SET_TARGET_PROPERTIES(test PROPERTIES LINK_SEARCH_END_STATIC 1)