set_property(DIRECTORY...) does not support setting COMPILE_OPTIONS and COMPILE_DEFINITIONS simultaneously
When specifying COMPILE_OPTIONS
and COMPILE_DEFINITIONS
simultaneously the specified COMPILE_OPTIONS also gets prepended by -D
resulting in the following error:
/usr/bin/cc -D-march=native -DFOO -MD -MT CMakeFiles/Test.dir/test.c.o -MF CMakeFiles/Test.dir/test.c.o.d -o CMakeFiles/Test.dir/test.c.o -c ../test.c
<command-line>:0:1: error: macro names must be identifiers
-D-march=native
is obviously wrong here. Example CMakeLists.txt
cmake_minimum_required(VERSION 3.17)
project(test)
set_property(DIRECTORY
PROPERTY COMPILE_OPTIONS -march=native
PROPERTY COMPILE_DEFINITIONS FOO
)
add_executable(test test.c)
When switching the PROPERTY
statements
set_property(DIRECTORY
PROPERTY COMPILE_DEFINITIONS FOO
PROPERTY COMPILE_OPTIONS -march=native
)
the error messages changes to
/usr/bin/cc FOO -march=native -MD -MT CMakeFiles/Test.dir/test.c.o -MF CMakeFiles/Test.dir/test.c.o.d -o CMakeFiles/Test.dir/test.c.o -c ../test.c
cc: error: FOO: No such file or directory
Splitting into 2 calls of set_property actually works:
set_property(DIRECTORY PROPERTY COMPILE_OPTIONS -march=native)
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS FOO)
/usr/bin/cc -DFOO -march=native -MD -MT CMakeFiles/Test.dir/test.c.o -MF CMakeFiles/Test.dir/test.c.o.d -o CMakeFiles/Test.dir/test.c.o -c ../test.c
Used CMake:
cmake version 3.18.0-rc4-g6b845e9
CMake suite maintained and supported by Kitware (kitware.com/cmake).
Edited by Alexander Stein