简体   繁体   中英

How to config cmake for strip file

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?

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.

你可以试试

set_target_properties(TARGET_NAME PROPERTIES LINK_FLAGS_RELEASE -s)

这工作正常:

add_link_options($<$<CONFIG:RELEASE>:-s>)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM