Sysprogs forums › Forums › VisualGDB › Recommended method for setting CMAKE_C_FLAGS_RELEASE
- This topic has 3 replies, 2 voices, and was last updated 2 years, 9 months ago by support.
-
AuthorPosts
-
February 24, 2022 at 11:06 #32314lateralorangeParticipant
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
February 24, 2022 at 18:57 #32315supportKeymasterHi,
Setting CMAKE_C_FLAGS_RELEASE from CMakeLists.txt should work just fine, as long as:
- It is done after the project() line
- 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.
February 25, 2022 at 10:04 #32321lateralorangeParticipantGreat, 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 🙂
February 25, 2022 at 10:52 #32327supportKeymasterNo problem and good to know it works.
-
AuthorPosts
- You must be logged in to reply to this topic.