how to prevent cmake from stripping the created sh

2019-07-23 16:59发布

问题:

When running the executable in the debugger, I don't see any meaningful stacktrace for the shared library -- but only the address of the function and the path of the shared library.

This applies to cmake version 3.7.2.

回答1:

  • CMake does not strip your debug symbols by default.
  • You need to compile your shared libs with proper debug options, e.g.

    cmake -DCMAKE_BUILD_TYPE=Debug ..
    
  • Or you can modify your CMakeLists.txt to add the debug flags.

    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall")
    

Edit

CMake is a build scripting tool, itself does not do stripping on your binary, but you can ask it to help with this if you want. See my other post here: Android NDK path variable for **strip** command in CMake build tool chain

Below lines will do symbol stripping if you want to let CMake to strip your debug symbols.

add_custom_command(TARGET ${SHARED_LIBRARY_NAME} POST_BUILD
            COMMAND "<path-to-your-bin>/strip" -g -S -d --strip-debug --verbose
            "<path-to-your-lib>/lib${SHARED_LIBRARY_NAME}.so"
            COMMENT "strip debug symbols done on final binary.")

For the warnings, you can choose to have it or not, doesn't really matter.

Get back to the question and clarify further, in order to have debug symbols, you need to build your binary in DEBUG or RelWithDebInfo build type by typing below cmake command.

cmake -DCMAKE_BUILD_TYPE=Debug ..

or

cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..

If you are building C source code (not the cpp which I assumed), then you can check the corresponding CMAKE_C_FLAGS.

See the official document from here: https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html



标签: linux cmake