2015-02-10 43 views
2

我編譯我的程序靜態,像這樣:CMake的使用錯誤的連接標誌(動態VS靜態)

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所有.a結束靜態 Boost庫之前。我如何強制CMAKE將它們與靜態標誌鏈接起來?

回答

2

LINK_SEARCH_END_STATIC爲目標屬性,你必須把它在目標本身:

SET_TARGET_PROPERTIES(test PROPERTIES LINK_SEARCH_END_STATIC 1)