I cannot alter the CMakeLists.txt of the project I work on and the cmake toolchain file used in there only declares C and CXX FLAGS for release.
So for debugging I need to force my own flags (namely to append -O0 -DDEBUG to the CFLAGS and CXXFLAGS).
So I tried through cli using the following example
main.cpp:
#include <iostream>
int main(int argc, char * argv[])
{
std::cout << "hello world\n";
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.1)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
#prescon settings
project(cmakecflags)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CXX_EXTENSIONS NO)
enable_testing()
add_executable(cmakecflagsapp
main.cpp
)
message(CFLAGS=${CFLAGS})
message(CXXFLAGS=${CXXFLAGS})
calling cmake:
CFLAGS=' -O0 -DDEBUG ' CXXFLAGS=' -O0 -DDEBUG ' cmake -G 'Unix Makefiles' -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=Debug .
the result as expected leaves the 2 variables empty
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CFLAGS=
CXXFLAGS=
-- Configuring done
-- Generating done
and me wondering.
I tried using cmake "--trace" to see what is being done and I could not find a relevant part where cmake would initialize its variables from same-named env variables.
Is there another variable I could use? perhaps another way ?
You can set the flags by using -DCMAKE_CXX_FLAGS="-O0 -DDEBUG"
As in:
cmake -DCMAKE_CXX_FLAGS=CFLAGS="-O0 -DDEBUG " -DCMAKE_C_FLAGS="-O0 -DDEBUG " -G 'Unix Makefiles' -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=Debug .