How to config cmake for strip file
Cleanest possible way is to modify CFLAGS or CXXFLAGS (depending on C or C++ code)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s")
But there is one more hack if you do not want to change your build system (figuring out exact place where to put above lines might be tricky). You may just use strip as standalone application, like:
strip -s a.out
and do this after executable is ready to release as a post-build step. I found this way cleaner, then disturbing compiler flags.
CMake: Platform independent binary stripping for release builds
In CMake
add_custom_command
can execute post build commands such as strip
. The PROJECT_NAME
variable is defined as your target binary e.g.
# Set the project name
set(PROJECT_NAME "MyProject")
project(${PROJECT_NAME})
Place the following code after add_executable
of your CMakeLists.txt
:
# Strip binary for release builds
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND $<$<CONFIG:release>:${CMAKE_STRIP} ${PROJECT_NAME}>)
CMAKE_STRIP
is the file path to the platform's strip
utility (e.g. /usr/bin/strip
on Linux
). The $<$<CONFIG:cfg>:str>
generator expression will expand to str
when building the configuration cfg
, and to an empty string otherwise. In this scenario, this directly means "call strip when building in Release, and do nothing otherwise". Note that the CONFIG
generator is case insensitive on your build type, and will work when using multi-config generators.
How to strip file extensions with cmake (or any other portable build tool)?
If you don't treat these script files as CMake targets, and instead treat them as files, you should be able to do:
project (Mixed Example)
add_executable (banana banana.cpp)
add_executable (grape grape.hs)
install (TARGETS banana grape DESTINATION bin)
if (UNIX)
install (FILES orange.py DESTINATION bin RENAME orange)
install (FILES strawberry.lua DESTINATION bin RENAME strawberry)
else (WIN32)
install (FILES orange.py strawberry.lua DESTINATION bin)
endif ()
If you want to use a function rather than calling install (FILES ...
multiple times, you can do:
function (install_files)
if (UNIX)
foreach (file ${ARGV})
get_filename_component (name_without_extension ${file} NAME_WE)
install (FILES ${file} DESTINATION bin RENAME ${name_without_extension})
endforeach ()
else (WIN32)
install (FILES ${ARGV} DESTINATION bin)
endif ()
endfunction ()
install (TARGETS banana grape DESTINATION bin)
install_files (orange.py strawberry.lua)
why is cmake producing a binary with debug_info and not stripped
there are some issues with setting flags in CMakeLists.txt
, so adding and using a toolchain file helped, and setting appropriate CMAKE_<LANG>_FLAGS_<CONFIG>_INIT
vars in the toolchain file. also, the linker is adding debug info. adding -s
flag to cmake release flags, and adding -Wl,--strip-debug
to linker flags is what i was looking for
here is my final CMakeLists.txt
and toolchain
files
#CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
# set toolchain file
set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/toolchain.cmake"
CACHE PATH "Path to the desired toolchain file.")
# project name
project(c-program C)
add_executable(c-program c-program.c)
# toolchain.cmake
# CMake toolchain file
# c compiler and standard
set(CMAKE_C_COMPILER "/usr/bin/gcc")
set(CMAKE_C_STANDARD "90")
set(CMAKE_C_STANDARD_REQUIRED YES)
# linker flags
set(CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,--strip-debug")
# cflags
set(CMAKE_C_FLAGS_INIT "-ansi -Wall")
# cflags for debug build
set(CMAKE_C_FLAGS_DEBUG_INIT "-g3 -ggdb3")
# cflags for release build
set(CMAKE_C_FLAGS_RELEASE_INIT "-O3 -DNDEBUG -s")
How to strip trailing whitespace in CMake variable?
This strips terminating newline in the variable <varname>:
string(REGEX REPLACE "\n$" "" <varname> "${<varname>}")
Works for one of the project I involved to since CMake 2.8.
Related Topics
How to Get the Address of the Std::Vector Buffer Start Most Elegantly
C++ Uniform_Int_Distribution Always Returning Min() on First Invocation
Visual Studio 2013: Cl.Exe Exited with Code -1073741515
C++ 2 Dimensional Array with Variable Size Rows
Google Mock - How to Call Expect_Call Multiple Times on Same Mock Object
Is There Any Automated Way to Implement Post-Constructor and Pre-Destructor Virtual Method Calls
How to Differentiate (When Overloading) Between Prefix and Postfix Forms of Operator++? (C++)
Code Runs 6 Times Slower with 2 Threads Than with 1
Can a Copy-Constructor Take a Non-Const Parameter
What Is the Practical Use of Pointers to Member Functions
Can Pointers Be of Different Sizes
How to Vertically Align Text in Edit Box
Why Are Designated Initializers Not Implemented in G++
Check If a Type Is Passed in Variadic Template Parameter Pack
How to Implement an Function Equivalent to Bwmorph Matlab Function in Opencv
Why Do I See 400X Outlier Timings When Calling Clock_Gettime Repeatedly
Redirect Both Cout and Stdout to a String in C++ for Unit Testing