Changing CMake compiler flags for multiple source

2019-07-18 18:42发布

I'm trying to debug a problem related to compiler optimisation (no issue with -O2 or below, segfault with -O3) and I'd like to be able to switch the compiler flags for a chunk of my source so I can try to narrow down where the segfault is coming from.

I can do this setting the global optimisation level to -O2, and altering the PROPERTIES for single files like so:

SET_SOURCE_FILES_PROPERTIES(file1.f90 PROPERTIES COMPILE_FLAGS -O3)

However, when I try to do this for multiple files using *.f90 for example, it seems to not work:

SET_SOURCE_FILES_PROPERTIES(*.f90 PROPERTIES COMPILE_FLAGS -O3)

Is there any way to do this for multiple files without specifying every file by name?

标签: cmake flags
2条回答
对你真心纯属浪费
2楼-- · 2019-07-18 19:26

Just adding to @ComicSansMS correct answer.

You could also use SET_SOURCE_FILES_PROPERTIES this way:

file(GLOB MyFiles *.f90)
SET_SOURCE_FILES_PROPERTIES(${MyFiles} PROPERTIES COMPILE_FLAGS -O3)

This code will have the same effect as

file(GLOB MyFiles *.f90)
set_property(SOURCE ${MyFiles} PROPERTY COMPILE_FLAGS -O3)
查看更多
ゆ 、 Hurt°
3楼-- · 2019-07-18 19:27

You can glob for a list of files:

file(GLOB MyFiles *.f90)
set_property(SOURCE ${MyFiles} PROPERTY COMPILE_FLAGS -O3)

Alternatively, you could set the COMPILE_FLAGS target property of the respective target instead. Usually, it does not make much sense to compile certain source files with different compile flags than others within the same target. So unless you have good reason to do this on a per-file basis, you should always use the target properties instead.

查看更多
登录 后发表回答