Compile flags, definitions, and debugging — CMake Workshop documentation (2024)

Objectives

  • How to get more information.

  • How to “print-debug” in CMake.

  • How to select the compiler.

  • How to see compile flags and definitions and how to control them.

  • How to configure for a debug build or a release build.

In the previous episode we have learned how to configure and build:

$ cmake -S. -Bbuild -DSOME_DEFINITION="something"$ cmake --build build

But many people (especially CMake old-timers like the presenter) do this instead with Unix Makefiles:

$ mkdir build$ cd build$ cmake .. -DSOME_DEFINITION="something"$ make

The former way is “better” because it is less tied to a specific build tool.

How to see all the flags and definitions in a verbose build

The way in which to trigger a verbose build depends on the native build tool you are using.For Unix Makefiles:

$ cmake --build build -- VERBOSE=1

But many people do this with Unix Makefiles:

$ mkdir build$ cd build$ cmake ..$ make VERBOSE=1

Also this works (on Unix Makefiles):

For Ninja:

$ cmake --build build -- -v

Hands-on: Verbose compilation

Try to see the verbose output from a compilation for our hello world example.You might need to “clean” to see a full re-build:

$ cmake --build build --target clean$ cmake --build build -- VERBOSE=1

Printing messages

You will most likely have to engage in debugging your CMake scripts at one pointor another. As in other languages, we often will go for the “print-debugging”using the message command:

message(STATUS "I am here!")message(FATAL_ERROR "I am here and I want the configuration to stop here.")

message can be a bit awkward to work with when printing variables:

message(STATUS "CMAKE_CXX_COMPILER_ID is ${CMAKE_CXX_COMPILER_ID}")message(STATUS "CMAKE_CXX_COMPILER_VERSION is ${CMAKE_CXX_COMPILER_VERSION}")

For this the built-in module CMakePrintHelpers comes in handy:

include(CMakePrintHelpers)cmake_print_variables(CMAKE_CXX_COMPILER_ID CMAKE_CXX_COMPILER_VERSION)

This would produce an output similar to this one:

-- CMAKE_CXX_COMPILER_ID="GNU" ; CMAKE_CXX_COMPILER_VERSION="9.3.0"

How can we select the compiler?

Hands-on: Printing compiler ID/vendor and version

Try to find out what compiler your CMake picked with:

include(CMakePrintHelpers)cmake_print_variables(CMAKE_CXX_COMPILER_ID)cmake_print_variables(CMAKE_CXX_COMPILER_VERSION)cmake_print_variables(SOMETHING_UNDEFINED)

Notice how the command did not fail but printed “” when asking for the value of SOMETHING_UNDEFINED.

We can specify the compilers by exporting the corresponding environment variables:

$ env FC=gfortran CC=gcc CXX=g++ cmake -S. -Bbuild$ cmake --build build

Or by setting CMake variables:

$ cmake -S. -Bbuild -DCMAKE_Fortran_COMPILER=gfortran -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++$ cmake --build build

Variables

These are either CMake- or user-defined. You can obtain the list of CMake-defined variables with:

$ cmake --help-variable-list

You can create a new variable with the set command:

set(MY_VARIABLE "some value")

Variables in CMake are always of string type, but certain commands can interpretthem as other types. If you want to declare a list variable, you will have toprovide it as a ;-separated string. Lists can be manipulated with the listfamily of commands.You can inspect the value of a variable by dereferencing it with the ${}operator, as in bash shell.

One of the most confusing aspects in CMake is the scoping of variables. There are three variable scopes in the DSL:

  • Function. In effect when a variable is set within a function: the variablewill be visible within the function, but not outside.

  • Directory. In effect when processing a CMakeLists.txt in a directory:variables in the parent folder will be available, but any that is set in thecurrent folder will not be propagated to the parent.

  • Cache. These variables are persistent across calls to cmake andavailable to all scopes in the project.Modifying a cache variable requires using a special form of the set function.

Here is a list of few CMake-defined variables:

Help on a specific built-in variable can be obtained with:

$ cmake --help-variable PROJECT_BINARY_DIR

Release and debug builds

CMake distinguishes the following build types:

  • Debug

  • Release

  • RelWithDebInfo

  • MinSizeRel

We can select the build type on the command line:

$ cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Debug$ cmake --build build

But it is often useful to set a default in the code:

# we default to Release build typeif(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "Release")endif()

Controlling compiler flags

We can define compiler flags for different compilers and build types:

if(CMAKE_Fortran_COMPILER_ID MATCHES Intel) set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall") set(CMAKE_Fortran_FLAGS_DEBUG "-g -traceback") set(CMAKE_Fortran_FLAGS_RELEASE "-O3 -ip -xHOST")endif()if(CMAKE_Fortran_COMPILER_ID MATCHES GNU) set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -Wall") set(CMAKE_Fortran_FLAGS_DEBUG "-O0 -g3") set(CMAKE_Fortran_FLAGS_RELEASE "-Ofast -march=native")endif()

Similarly you can set CMAKE_C_FLAGS and CMAKE_CXX_FLAGS.

However, this will set the flags for the entire project. If you wantfine-grained control, a nicer way is to define compile flags per targetlike in this example (here we want to lower the optimization level for mytargetto -O1):

target_compile_options(mytarget PRIVATE -O1 )

Targets were mentioned only briefly, we talk more about them in the next episode.

Compile flags, definitions, and debugging — CMake Workshop  documentation (2024)

FAQs

How to compile with debug CMake? ›

To run a C++ debugger, you need to set several flags in your build. CMake does this for you with “build types”. You can run CMake with CMAKE_BUILD_TYPE=Debug for full debugging, or RelWithDebInfo for a release build with some extra debug info.

How to use flag in CMake? ›

The recommended way to set C++ flags in modern CMake is to use per-target flags which can be populated to other targets through the target_compile_definitions() function. This will populate the INTERFACE_COMPILE_DEFINITIONS for the library and push the definition to the linked target depending on the scope.

What is CMake build type release? ›

Specifies the build type on single-configuration generators (e.g. Makefile Generators or Ninja ). Typical values include Debug , Release , RelWithDebInfo and MinSizeRel , but custom build types can also be defined.

How do I compile a source file with debug information? ›

Generally, you want to compile your source files with the -g option to make your program more debuggable. The -g option causes the compilers to record debugging information (in stabs or Dwarf format) into the object files along with the code and data for the program.

How do you debug a compiled program? ›

You can use a symbolic debugger to debug applications compiled with XL C/C++. At compile time, you can use the -g or -qlinedebug option to instruct the XL C/C++ compiler to include debugging information in compiled output.

What is the compiler flag? ›

Compiler flags are options you give to gcc when it compiles a file or set of files. You may provide these directly on the command line, or your development tools may generate them when they invoke gcc. This section describes just the flags that are specific to Objective-C.

What do flags do in C++? ›

Flag variable is used as a signal in programming to let the program know that a certain condition has met. It usually acts as a boolean variable indicating a condition to be either true or false.

What is flag in Makefile? ›

There is no convenient way to override the options without modifying the makefile . This is where make flags come into play. Flags in make are just variables containing options that should be passed to the tools used in the compilation process.

What is the difference between CMake and CMake? ›

CMake and Make work on different principles. With CMake you specify build targets, and assign source files and various parameters. With Make, you create a set of build rules. Each rule has a target file, a set of dependencies (to the right of the colon), and the commands needed to perform the rule's build task.

Is release build faster than debug? ›

For production code, the release build (and JIT) eliminates code that only exists to support breakpoints, resulting in a faster JIT and smaller, faster code. This is why the IDE prefers the debug build – the ability to set arbitrary breakpoints on any reachable code.

What is the difference between debug and release build configuration? ›

A Debug configuration supports the debugging of an app, and a Release configuration builds a version of the app that can be deployed. For more information, see How to: Set debug and release configurations. You can also create custom solution configurations and project configurations.

How do I run a build in debug mode? ›

Change the build configuration
  1. On the toolbar, choose either Debug or Release from the Solution Configurations list. or.
  2. From the Build menu, select Configuration Manager, then select Debug or Release.
Jan 19, 2024

How do I run Makefile in debug mode? ›

Finally, you can also debug the Makefile by running Make with the debug flag: make -d . This will print all the rules (including built-in ones) that Make tries for each of the targets, and whether or not a rule needs to be run.

How to compile using Cmakelist? ›

Sure, here's a concise version:
  1. Create CMakeLists. txt:cmakeCopy code cmake_minimum_required(VERSION 3.10) project(YourProjectName) add_executable(YourExecutableName main. cpp)
  2. Build:bashCopy code mkdir build cd build cmake .. make.
  3. Run:bashCopy code ./YourExecutableName.
Feb 1, 2023

How do I run a script in debug mode? ›

On the activity bar, click the Run and Debug icon or use the keyboard shortcut Ctrl+Shift+D. From the Configuration list in the Run view, click the Debug Script configuration. Set the launch configuration and breakpoints.

References

Top Articles
U Haul's Near Me
Launcher Update and New Debug Functionality
ᐅ eGirl Kleidung kaufen: Wie ein eGirl aussehen so funktionierts!
Antonym For Proton
People Helping Others Property
Guardians Of The Galaxy Showtimes Near Athol Cinemas 8
Unveiling The Voice Behind Maui: The Actor Behind The Demigod
Academic Calendar Biola
Mistar Student Portal Southfield
Walmart Front Door Wreaths
Milk And Mocha Bear Gifs
Lebron Vs Pacers Stats
Pokewilds Wiki
New & Used Motorcycles for Sale | NL Classifieds
Making a Docker Container Use a VPN – Natural Born Coder
Sofia the baddie dog
Lowell Holiday Wrestling Tournament 2022
月曜から夜ふかし 9Tsu
Justine Waddell talks about a season of screenings MELODIA!
Pixel Speedrun Unblocked Games 76
ZQuiet Review | My Wife and I Both Tried ZQuiet for Snoring
Sour Animal Strain Leafly
Ebony Pyt Twerk
Craigs List Jonesboro Ar
Myworld Interactive American History Pdf
Meridamoonbeams
Www.dunkin Baskin Runs On You.com
Monahan's By The Cove Charlestown Menu
The Real-Life Woman That Inspired Japanese Horror Classic Ring - SlashFilm
Retire Early Wsbtv.com Free Book
Kahoot Spamming Bots
Emojiology: 🤡 Clown Face
Receive Sms Verification
Connection | Scoop.it
Affordable Prom Dresses for Women | Fashion Nova
100000 Divided By 3
Valentino Garavani Flip Flops
Was Lil Mosey In Ride Along
Minor League Baseball Leaders
Lvpg Orthopedics And Sports Medicine Muhlenberg
Rush Copley Swim Lessons
5417873087
Grasons Estate Sales Tucson
Sak Pase Rental Reviews
Pinellas Fire Active Calls
Erica Mena Net Worth Forbes
Antonin Balthazar Lévy
Eureka Mt Craigslist
Toothio Login
Westside Veterinary Hospital Arab Photos
Sarah Colman-Livengood Park Raytown Photos
Craigslist Org Las Vegas Cars
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 5315

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.