0

I want to using cmake toolchain to configure compiler and compiler flags. I want to using the varible to configure compile. I can not figure out why I can not change the compile flags in cmake toolchains. I modify the CMAKE_C_FLAGS_RELEASE to -O2, but the result shown is -O3 -NDEBUG, which is the default value in cmake.

The case shown as follows.

# CMakeLists.txt
set(CMAKE_TOOLCHAIN_FILE MyToolChain.cmake)

project(MyProject)

message(STATUS "c debug:"   ${CMAKE_C_FLAGS_DEBUG})
message(STATUS "c release:" ${CMAKE_C_FLAGS_RELEASE})

and

# MyToolChain.cmake
message(STATUS "-------")

set(CMAKE_C_COMPILER   gcc)
set(CMAKE_CXX_COMPILER g++)

set(CMAKE_C_FLAGS_DEBUG   -O2)
set(CMAKE_C_FLAGS_RELEASE -O2)

I run the cmake, I create a build directory

mkdir build
cd build
cmake ..

The result output is

-- -------
-- -------
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- 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/g++
-- Check for working CXX compiler: /usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- c debug:-g                                 #comments: not changed, I set to -O2
-- c release:-O3 -DNDEBUG                     #comments: not changed, I set to -O2
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xuhui/tmp/ad/build

The cmake version I use is cmake 3.10.

Any suggestions? Thanks for your time.

Xu Hui
  • 1,213
  • 1
  • 11
  • 24
  • 1
    In a toolchain file, compiler flags should be set as a **cache** variable. Or, better, as a `_INIT` suffixed variable. – Tsyvarev Mar 06 '23 at 13:55

0 Answers0