0

This is probably a very simple thing but I can't find an answer for it.

For example I have the following compilation option in CMakeLists.txt

set(CMAKE_CXX_FLAGS_DEBUG "-O1 -g")

How can I use it? Doing

make CMAKE_CXX_FLAGS_DEBUG

fails.

I also can't directly do

make -O1 -g

CMakeLists.txt (shortened a bit and the name of the project anonymized):

cmake_minimum_required(VERSION 3.8)

# Set compiler
set(CMAKE_CXX_COMPILER "clang++")
set(CMAKE_C_COMPILER "clang")

# Set compilation options
set(CMAKE_CXX_FLAGS_DEBUG_INIT "-Wall -O0 -g")
set(CMAKE_CXX_FLAGS_RELEASE_INIT "-Wall")

set(CMAKE_BUILD_TYPE Debug)

# Set path to find additional dependencies
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

# Version of the std library
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)


# options for compilation
option(BUILD_AAAA "Build ..." ON)

# Set names of libraries
set(LIB_AAAA ${CMAKE_PROJECT_NAME})

if (BUILD_AAAA)
    message(STATUS "Building sources.")
    add_subdirectory(src)
endif()




# Configure AAAA.pc file and install it
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/AAAA.pc.in ${CMAKE_CURRENT_BINARY_DIR}/AAAA.pc @ONLY)    

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/AAAA.pc    
        DESTINATION lib/pkgconfig/)

1 Answer 1

1

If you define a target in cmake after setting variable, cmake initializes the properties of the target with the value of the variable, if you specify the right build type. No make options necessary; (I recommend using cmake --build . instead anyways to be build system independent.)

...
set(CMAKE_CXX_FLAGS_DEBUG "-O1 -g")

# with the correct configuration the flags are automatically applied to the following executable
add_executable(HelloWorld main.cpp)

Command line (assuming the working directory containing CMakeLists.txt and are on Linux):

mkdir buildDebug
cmake -D CMAKE_BUILD_TYPE=Debug -S . -B buildDebug
cmake --build buildDebug

The second command sets up the project inside the buildDebug directory with the debug configuration and the third command builds the project (equivalent to make all run from inside buildDebug.


Edit after question update

You're setting the build type to Debug in the CMakeLists.txt file. I don't recommend doing this, since this prevents you from setting up the project with another configuration. If you want Debug to be the default configuration, I'd use a cache variable. There's a similar issue with setting the compiler in the CMakeLists.txt file; for setting the compiler you may be better of using a toolchain file.

As an alternative create a cmake script initializing cache variables with the desired values which allows you to pass multiple values with just passing the file name via -C option to cmake which may be a conventient way of providing a set of possible configurations.

The only thing that changes in the answer though is the fact that passing the build type via command line is not possible in the current state of the cmake project, i.e. -D CMAKE_BUILD_TYPE=Debug could be removed from the second command line command.

2
  • Thanks for your reply, however I still don't get it. I added a shortened version of the CMakeLists.txt, can you take a look at it? There's no "add_executable" in it, but I'd guess that it's managed some other way, since the project compiles and works. When I change the options (-O0 to -O1 for example) it seems to have no effect. I'm using cmake without the option CMAKE_BUILD_TYPE=Debug since it's already in CMakeLists.txt.
    – Polzarak
    Commented May 1, 2021 at 10:04
  • @Polzarak I assume some library or executable is created in the subdirectory. The subdirectory has access to variables set in the parent directory scope unless you introduce a variable with the same name in the subdirectory. Assuming neither CMAKE_BUILD_TYPE nor CMAKE_CXX_FLAGS_DEBUG_INIT get overwritten in the subdirectory, the answer should still apply. You could simply check by printing the variable just before the command creating the target (add_executable/ add_library) using message(STATUS "CMAKE_CXX_FLAGS_DEBUG_INIT used for target x: ${CMAKE_CXX_FLAGS_DEBUG_INIT}")
    – fabian
    Commented May 1, 2021 at 10:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.