Recommended method for setting CMAKE_C_FLAGS_RELEASE

Sysprogs forums Forums VisualGDB Recommended method for setting CMAKE_C_FLAGS_RELEASE

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #32314
    lateralorange
    Participant

    Hello,

    I ran into a bug today, that was caused by the GCC compiler using -O3, when I had meant to set it as -Os. Normally I would set the CFLAGS using a statement in the CMakeLists.txt file like:

    SET(CMAKE_C_FLAGS_RELEASE "-Os" CACHE INTERNAL "c compiler flags release")
    SET(CMAKE_CXX_FLAGS_RELEASE "-Os" CACHE INTERNAL "cxx compiler flags release")
    SET(CMAKE_ASM_FLAGS_RELEASE "" CACHE INTERNAL "asm compiler flags release")
    SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "-Wl,-Map=output.map" CACHE INTERNAL "linker flags release")

    However, I noticed that it was being overridden by a CMake file from VisualGDB, located here:

     C:\Program Files (x86)\Sysprogs\VisualGDB\CMake\embedded\root.cmake(20): set(CMAKE_C_FLAGS_RELEASE "-g3 -O3"

    I was able to fix it by defining OPTIMIZE_FOR_SIZE, which I did because I saw this snipped in the root.cmake file:

    if("${VISUALGDB_TOOLCHAIN_SUBTYPE}" STREQUAL "GCC")
    set(CMAKE_C_FLAGS_DEBUG "-g3 -O0")
    set(CMAKE_CXX_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})
    
    #Add "OPTIMIZE_FOR_SIZE=1" to Extra CMake Configuration Variables in VisualGDB Project Properties in order to optimize the release build for size, rather than speed.
    if(OPTIMIZE_FOR_SIZE)
    set(CMAKE_C_FLAGS_RELEASE "-g3 -Os")
    else()
    set(CMAKE_C_FLAGS_RELEASE "-g3 -O3")
    endif()
    
    set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})
    endif()

    However, I was wondering if there is a better way to set this, since I may need to release a binary using -O1 or -O2

    I tried searching through the tutorials, but couldn’t find what I’m looking for.

    The project is using the GCC toolchain, and the Advanced CMake project type, for an ARM Cortex M4 STM32L4-Series MCU (arm-none-gnueabi-)

    Is there a recommended method/configuration page for overriding CFLAGS?

    Thanks,

    LO

    #32315
    support
    Keymaster

    Hi,

    Setting CMAKE_C_FLAGS_RELEASE from CMakeLists.txt should work just fine, as long as:

    1. It is done after the project() line
    2. It does not specify any extra attributes (e.g. CACHE)

    If it doesn’t work, please share your CMakeLists.txt file and a sample gcc command line (you can force ninja to print the failed command line by adding an #error directive in the source file) and we will investigate it further.

    #32321
    lateralorange
    Participant

    Great, just tried your solution, works just as you said. Thanks!

    Good to know about the #error directive for printing the command line invocation, I’ll keep that in mind. I normally add:

    SET(CMAKE_EXPORT_COMPILE_COMMANDS 1)

    Which creates compile_commands.json, and has a list of every single gcc invocation, and can be used with other stuff like SourceTrail.

    Anyways, thanks for the support, really enjoying VisualGDB 🙂

    #32327
    support
    Keymaster

    No problem and good to know it works.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.