方法1
cmake生成的Makefile中有一个target名为intall/strip可以将install的可执行程序执行strip,执行make help,就可以看到
$ make help
The following are some of the valid targets for this Makefile:
... all (the default if no target is provided)
... clean
... depend
... install
... list_install_components
... install/strip
... install/local
... rebuild_cache
... edit_cache
所以执行make install/strip安装程序时就会自动执行strip
如果要深究细节,可以查看Makefile代码,install/strip 是这样写的
install/strip: preinstall
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Installing the project stripped..."
/opt/toolchains/mips-gcc520-glibc222/bin/cmake -DCMAKE_INSTALL_DO_STRIP=1 -P cmake_install.cmake
.PHONY : install/strip
上面的代码可以看出安装动作实际是由cmake_install.cmake来实现的,
上面install/strip执行cmake时调用的脚本 cmake_install.cmake中会根据CMAKE_INSTALL_DO_STRIP的值决定是否执行strip命令,如下是cmake_install.cmake脚本中的代码片段
foreach(file
"$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/./app-1.3.1"
"$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/./app"
)
if(EXISTS "${file}" AND
NOT IS_SYMLINK "${file}")
if(CMAKE_INSTALL_DO_STRIP)
execute_process(COMMAND "/opt/toolchains/mips-gcc520-glibc222/bin/mips-linux-gnu-strip" "${file}")
endif()
endif()
方法2
网上找到这个stackoverflow上的帖子《How to config cmake for strip file》,才知道gcc本身有一个-s连接选项(Link Option)用于删除执行程序的符号表和重定位信息。

参见gcc官方手册《3.14 Options for Linking》


本文详细介绍了如何使用CMake的Makefile中的install/strip目标,自动执行strip命令以删除安装程序的符号表和重定位信息,同时提供了通过gcc的-s选项实现相同目的的方法。
1365

被折叠的 条评论
为什么被折叠?



