Cartesius00 Cartesius00 - 1 month ago 16
C Question

Debug vs Release in CMAKE

In a GCC compiled project,


  • how to run
    cmake
    for each target type (debug/release)?

  • how to specify debug and release C/C++ flags using
    cmake
    ?

  • how to express that the main executable will be compiled with
    g++
    and one nested library with
    gcc
    ?


Answer

It's generally best to do an "out of source" build. Create your CMakeLists.txt in the root of your project. Then from the root of your project:

mkdir Release
cd Release
cmake -DCMAKE_BUILD_TYPE=Release ..
make

And for Debug (again from the root of your project):

mkdir Debug
cd Debug
cmake -DCMAKE_BUILD_TYPE=Debug ..
make

Debug will add the debug flags appropriate for your compiler. See this FAQ for more details.


You can modify/add to the flags in your CMakeLists.txt via CMAKE_C_FLAGS_DEBUG and CMAKE_C_FLAGS_RELEASE variables, e.g.:

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall")

See this link under CMAKE_BUILD_TYPE for more details.


As for your 3rd question, I'm not sure what you are asking exactly. CMake should automatically detect and use the compiler appropriate for your different source files.

Comments