0

I am trying to cross-compile a "Release" version of C++ based application using cmake and make. Its for Makefile based generators. But, somehow passing the command -DCMAKE_BUILD_TYPE=RELEASE is ignored by the cmake. I have also added below commands to CMakeLists.txt file.

add_compile_options(
        $<$<CONFIG:DEBUG>:-g3>
        $<$<CONFIG:DEBUG>:-Og>
        $<$<CONFIG:RELEASE>:-O3>
        )

add_compile_definitions(
        $<$<CONFIG:RELEASE>:RELEASE>
        )

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

In the CMakeCache.txt file I could see CMAKE_BUILD_TYPE:STRING=RELEASE is set. I am calling below commands to cross-compile:

mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=RELEASE ..
make

Configuration is successful and make is also successful. But generated file is with debug info. I want to generate without debug info(Release config)

ELF 32-bit LSB shared object, ARM, EABI5 version 1 (GNU/Linux), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, BuildID[sha1]=cc8fff.., for GNU/Linux 3.2.0, with debug_info, not stripped

My goal is to strip this debug info.

I also tried with add_custom_commands to strip binary as part of POST_BUILD using CMAKE_STRIP. In the CMakeLists.txt

add_custom_command(
TARGET "${TARGET_NAME}" POST_BUILD
DEPENDS "${TARGET_NAME}"
COMMAND $<$<CONFIG:RELEASE>:${CMAKE_STRIP}>
ARGS --strip-all $<TARGET_FILE:${TARGET}>
)

This is added after add_executable section. But while configuration I am getting below error from cmake

add_custom_command Wrong syntax.  A TARGET and OUTPUT can not both be specified.

Can anyone please let me know what is the correct way to generate Release version of binary using cmake?

Your help will be much appreciated.

Thanks in advance.

P.S: I am working on Ubuntu machine. Please let me know if any info is missing here.

Preeti
  • 535
  • 1
  • 6
  • 30
  • For debug purposes you could always run `make` with additional `V=1` option and check which exact parameters are passed to the compiler or linker. – Tsyvarev Jun 03 '22 at 07:00
  • POST_BUILD is already dependent on TARGET, remove DEPENDS. – 273K Jun 03 '22 at 07:05
  • I would vote for duplicate https://stackoverflow.com/questions/53692348/cmake-platform-independent-binary-stripping-for-release-builds – 273K Jun 03 '22 at 07:05
  • For striping debug info use -s link option. – 273K Jun 03 '22 at 07:07
  • 2
    Why are you using upper case in `RELEASE`/`DEBUG`? The convention is `Release`/`Debug`. That has nothing to do with your problem, just an observation. – ixSci Jun 03 '22 at 07:13
  • Hello @273K, I also tried with `add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND $<$:${CMAKE_STRIP} ${PROJECT_NAME}>)` But I get same error. Can you please let me know how to use `-s` link to strip debug info? This would be helpful – Preeti Jun 03 '22 at 07:19
  • @ixSci, thanks for the info. But I already tried with `Release/Debug`. Its same result – Preeti Jun 03 '22 at 07:20
  • Why are you striping project name? It should be target file. – 273K Jun 03 '22 at 07:27
  • 1
    Please show a [mre], setting the build type to release should produce an executable without debug info without having to specify any compile options. Is the debug info perhaps coming from one of the libraries you are linking to? – Alan Birtles Jun 03 '22 at 07:27
  • @Tsyvarev, I could not find any parameters specific to CMAKE_BUILD_TYPE using `make V=1` or `make VERBOSE=1` – Preeti Jun 03 '22 at 07:32
  • @273K, sorry that solution was copied from https://stackoverflow.com/questions/53692348/cmake-platform-independent-binary-stripping-for-release-builds – Preeti Jun 03 '22 at 07:33
  • See https://stackoverflow.com/questions/38675403/how-to-config-cmake-for-strip-file for -s – 273K Jun 03 '22 at 07:37
  • " I could not find any parameters specific to CMAKE_BUILD_TYPE using `make V=1` or `make VERBOSE=1`" - Well, it smells like your `add_compile_options` doesn't affect on the target which you want, doesn't it? So the **actual** problem is not about CMAKE_BUILD_TYPE, but about *something else*. Once again, we need to see [mcve], not just a several lines of code. – Tsyvarev Jun 03 '22 at 08:29
  • Hi @273K, after including -s, binary is stripped. I included `set_target_properties(TARGET_NAME PROPERTIES LINK_FLAGS_RELEASE -s)` in `CMakeLists.txt`. I wanted to know whether if this a safer option. And also now it is showing `ARM, EABI5 version 1 (SYSV)`, earlier it was `ARM, EABI5 version 1 (GNU/Linux)`. I hope this is fine. – Preeti Jun 03 '22 at 08:29
  • did you want `MinSizeRel` ? ref: https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html – Mizux Jun 03 '22 at 11:29

0 Answers0