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

Pause#

  • Give trainer time to switch project to C++

Class Diagram#

@startuml

interface Greeter {
  + sayhello()
}

class SimpleGreeter {
  + sayhello()
}
class NameGreeter {
  + sayhello()
  name: std::string
}

Greeter <|.. SimpleGreeter
Greeter <|.. NameGreeter

@enduml

C++ Standard Version#

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
$ 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