CMake cross-compiling: C flags from toolchain file ignored

ejoerns picture ejoerns · Jul 11, 2012 · Viewed 13.8k times · Source

I use cmake for cross compiling. In my toolchain file there is a line

SET(CMAKE_C_FLAGS "-std=gnu99")

This variable is not set in CMakeLists.txt again.

When I run cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake .. this flag is ignored.

To be more detailed: The line of flags.cmake shows an empty C_FLAGS = line. But in CMakeOutput.log I can find a line Build flags: -std=gnu99.

I found out that a second run of cmake .. (same with or without toolchain file specified) fixes this problem.

But why is it not set the first time i run cmake ??

EDIT: Added MNWE

CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)
project(myproject)

SET(files src/main.c)

add_executable(myexec ${files})

avr.cmake:

SET(CMAKE_SYSTEM_NAME Generic)

SET(CMAKE_C_COMPILER avr-gcc)

SET(CMAKE_C_FLAGS "-std=gnu99")

Answer

ejoerns picture ejoerns · Jul 18, 2012

I've found a temporary solution by replacing the line

SET(CMAKE_C_FLAGS "-std=gnu99")

by

SET(CMAKE_C_FLAGS "-std=gnu99" CACHE STRING "" FORCE)