C++, Debug/Release, CMake “Programming”, Rants

Pause

  • Give trainer time to switch project to C++

Class Diagram

../../../../../_images/hierarchy.png

C++ Standard Version

SET(CMAKE_CXX_STANDARD 23)
$ make VERBOSE=1
... /usr/bin/c++ -std=gnu++23 ...

Debug Vs. Release Builds: CMAKE_BUILD_TYPE

  • Debug

    $ cmake -DCMAKE_BUILD_TYPE=Debug /home/jfasch/work/jfasch-home/trainings/material/soup/cmake/09-c++/
    $ make VERBOSE=1
    ... /usr/bin/c++ -O3 -DNDEBUG ...
    

    (Looks pretty arbitrary and half-hearted)

  • Release

    $ cmake -DCMAKE_BUILD_TYPE=Release /home/jfasch/work/jfasch-home/trainings/material/soup/cmake/09-c++/
    $ make VERBOSE=1
    ... /usr/bin/c++ -O3 -DNDEBUG ...
    

    (Looks pretty arbitrary and half-hearted)

Compiler Type

  • Compiler flags chosen half-heartely by CMake

  • ⟶ custom flags needed

  • E.g. for “Debug”, but only if GCC

    • Optimization off (-O0), to improve single-stepping experience

    • Better debug info (-g3)

    • More warnings (-Wall) for sanity

    • Turn warnings into errors (-Werror) for sanity

IF (${CMAKE_BUILD_TYPE} STREQUAL Debug)
  IF (${CMAKE_C_COMPILER_ID} STREQUAL GNU)
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -g3 -Wall -Werror")
  ENDIF()
  IF (${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g3 -Wall -Werror")
  ENDIF()
ENDIF()

Strings And Lists

  • CMake’s “language” has no type system

  • Strings can be compared numerically or lexically ⟶ no errors, just bugs

  • Lists are strings that contain semicolon separated values

  • ⟶ CMake commands to manipulate strings and lists

IF (${CMAKE_BUILD_TYPE} STREQUAL Debug)
  MESSAGE(DEBUG "Oida! Debug Build!!")
  IF (${CMAKE_C_COMPILER_ID} STREQUAL GNU)
    STRING(APPEND CMAKE_C_FLAGS "-O0 -g3 -Wall -Werror")
  ENDIF()
  IF (${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
    STRING(APPEND CMAKE_CXX_FLAGS "-O0 -g3 -Wall -Werror")
  ENDIF()
ENDIF()
  • Show MESSAGE() usage …

$ cmake --log-level=Debug  -DCMAKE_BUILD_TYPE=Debug /home/jfasch/work/jfasch-home/trainings/material/soup/cmake/09-c++/
-- Oida! Debug Build!!
...

MESSAGE() (A.k.a printf() Debugging)

  • Basic usage

    MESSAGE("Howdy")
    

    is the same as

    MESSAGE(NOTICE "Howdy")
    

    is the same as

    MESSAGE(Howdy)          # <--- root of all evil (one of roots)
    

⟶ all sorts of … crap:

  • Tags? Enums? No!

    MESSAGE(WARNING "Howdy")
    

    Prints, as expected …

    CMake Warning at CMakeLists.txt:5 (MESSAGE):
      Howdy
    
    MESSAGE(BULLSHIT "Howdy")
    

    Prints …

    $ cmake ~/work/jfasch-home/trainings/material/soup/cmake/code/
    BULLSHITHowdy