Changing CMake compiler flags for multiple source

2019-07-18 18:34发布

问题:

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?

回答1:

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.



回答2:

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)


标签: cmake flags