How to config cmake for strip file
Asked Answered
K

5

32

when I use cmake in Release mode I have the following binary:

64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=485ac09b0a3aa879f88b7f5db6c00ea8d8e1eaf6, not stripped

I want the binary to be stripped. How can I say to cmake in a clean way to add the -s option to my compiler to make it stripped?

Why did the Default Release mode not strip my binary?

Kandis answered 30/7, 2016 at 15:9 Comment(2)
Note that when you install a CMake project via cmake --install, you can provide the --strip argument that will strip everything upon installation. Doing things this way is typically much cleaner. You shouldn't deploy from the build folder, anyway, since the RPATHs will be messed up.Nicolettenicoli
Yes --strip is the standard way to do it.Computerize
U
33

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.

Undershorts answered 30/7, 2016 at 16:21 Comment(7)
Instead of altering you files, you can also pass -DCMAKE_C_FLAGS="-s" to the CMake call.Mattison
Agree. Better -DCMAKE_C_FLAGS_RELEASE if you want to strip only release build. But this might be dangerous if cmake build overwrites flags instead of adding to them (regular case for custom builds).Undershorts
Copy-pasted that 2 lines into my CMakeLists.txt. Makes no difference. Meanwhile a standalone /usr/bin/strip reduced the binary size to 55% of the original.Incurable
This doesn't work if you link to library with debug symbols. It is better to strip on link, not in CXX.Wordbook
You don't need this for MSVS. With Visual C++ (and other Microsoft compilers) on Windows, symbols aren't part of the binaries. Instead, they are stored in separate files called "Program Database" files (.pdb files).Undershorts
Work well on gcc but not on Clang , because it doesn't have -s :( For compiler independent solution, check hereHypnosis
One positive to @Dmitry's answer is that this argument (-s) is only added to the link stage. Meaning, by adding it to the C or CXX flags, every source file that is compiling will have these new flags. This doesn't happen when you use add_link_options. I didn't test, but this might help the situation with Clang, too. This might also address the other comment about stripping on link not in CXX.Deck
F
26

You can try

set_target_properties(TARGET_NAME PROPERTIES LINK_FLAGS_RELEASE -s)
Freiburg answered 6/5, 2019 at 10:19 Comment(3)
This is better than just setting the compiler flags as this affects only the desired target not all targetsGlori
@وليدتاجالدين - it is actually much worse since it locks your project into compilers that understand the -s flag with the same meaning as you intended.Nicolettenicoli
@AlexReinking - I mean better than the accepted answer - for my case that doesn't need to strip all the targets - as the accepted answer is already using the 's' flag. I think that your comment on the question will be better for stripping all the targets.Glori
E
11

Using add_link_options() or set_target_properties() to add -s should work fine, additionally, CMake creates an install/strip target which also could be used for striping the binary if you have at least one install() command for your target (reference).

Example:

$ cmake --build . --config Release --target install/strip
Elusion answered 8/4, 2021 at 5:33 Comment(0)
A
8

This works fine:

add_link_options($<$<CONFIG:RELEASE>:-s>)
Askwith answered 17/2, 2021 at 14:2 Comment(0)
E
0

This also worked for me

set(CMAKE_EXE_LINKER_FLAGS "-s")

Invoking strip only at linking time.

Estrella answered 2/6 at 7:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.