2016-03-14 68 views
0

我有Ubuntu 14.04/GCC 4.8/CMake 2.8內置的應用程序,內含-static-libgcc和-static-libstdC++。 LDD命令顯示:通過CMake 2.8靜態鏈接Linux C++應用程序

linux-vdso.so.1 => (0x00007ffc16195000) 
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fcb18256000) 
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fcb17f50000) 
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcb17b8a000) 
/lib64/ld-linux-x86-64.so.2 (0x000055fc367e1000) 

當我添加-static到CMAKE_EXE_LINKER_FLAGS - 我從LDD輸出不同:

linux-vdso.so.1 => (0x00007ffd48349000) 
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fe8d0335000) 
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fe8d0031000) 
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fe8cfd2a000) 
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe8cf965000) 
/lib/ld64.so.1 => /lib64/ld-linux-x86-64.so.2 (0x000055d61ec7d000) 
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fe8cf74f000) 

我應該-static標誌將導致靜態鏈接的應用程序。它沒有發生。 任何人都可以解釋我爲什麼?

我有生成的Qt Creator生成文件 - 靜態開關生成結果在完全靜態的應用程序。

回答

0

它來自CMakeLists.txt。運行CMake時使用-DSTATIC_LINKING = True。

IF(STATIC_LINKING) 
    SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a") 
ENDIF(STATIC_LINKING) 


# libpcap library 
find_library(pcap libpcap.a) 

# opencore-amr libraries 
find_library(opencore-amrnb libopencore-amrnb.a) 
find_library(opencore-amrwb libopencore-amrwb.a) 

# c++ netlib 
find_library(cppnetlib-server-parsers libcppnetlib-server-parsers.a) 
find_library(cppnetlib-uri libcppnetlib-uri.a) 
find_library(cppnetlib-client-connections libcppnetlib-client-connections.a) 

# pthread 
find_library(pthread libpthread.a) 

# boost libraries 
set(Boost_USE_STATIC_LIBS ON) 
set(Boost_USE_MULTITHREADED ON) 

# set -static, when STATIC_LINKING is TRUE and set LINK_SEARCH_END_STATIC 
# to remove the additional -bdynamic from the linker line. 
IF(STATIC_LINKING) 
    SET(CMAKE_EXE_LINKER_FLAGS "-static -static-libgcc -static-libstdc++" 
    SET_TARGET_PROPERTIES(pvqa-server PROPERTIES LINK_SEARCH_END_STATIC 1) 
ENDIF(STATIC_LINKING) 
0

-static-static-libgcc-static-libstdc++不同,具體地說-static不假定標準庫的靜態鏈接。這兩個標誌控制着這種行爲。

要與標準庫靜態鏈接,請將這兩個標誌添加到CMake CMAKE_EXE_LINKER_FLAGS變量中。

+0

這些標誌已在CMAKE_EXE_LINKER_FLAGS中設置。真正的問題是,結果可執行文件仍然依賴於Linux動態庫(請參閱上面的post中的ldd輸出)。我的目標是完全靜態構建。 – Dmytro