cmake静态编译(Linux)

首先安装libstdc,glibc静态库

yum install libstdc++-static  glibc-static -y

CMakefile中增加 set(CMAKE_EXE_LINKER_FLAGS “-static”) 即可

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
 
PROJECT(cmake1)
  
add_executable(cmake1 MACOSX_BUNDLE cmake1.cpp)

set(CMAKE_EXE_LINKER_FLAGS "-static")

截图说明:

cmake静态编译

如截图所示,ldd cmake1 显示为 “not a dynamic executable”

golang静态编译(Linux)

更加简单

cgo_enable=0 go build

截图说明:

golang静态编译

如截图所示,ldd cmake1go 显示为 “not a dynamic executable”

备注详细测试用例:

cmake静态编译 https://github.com/panyingyun/cmakedemo/tree/master/cmake8/cmake1

golang静态编译 https://github.com/panyingyun/cmakedemo/tree/master/cmake8/cmake1go