cmake/gcc:strip缩减程序体积

版权声明:本文为博主原创文章,转载请注明源地址。 https://blog.csdn.net/10km/article/details/83212925

方法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》

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/10km/article/details/83212925