I'm using CMake, and I want to add a compilation flag to some flags variable. For example, I want to add -DFOO
to the CMAKE_CXX_FLAGS_RELEASE
variable.
Right now, I use:
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DFOO" )
... but if there already is a -DFOO
flag, I get it double, which might be harmless but I'd rather avoid it. Assuming I can't control whether or not there's a -DFOO
to begin with - how can I "add a flag only if it's missing" to such a flags variable?
Notes:
- An answer regarding adding elements to a space-separated-list variable in general will suffice, I guess.
- My
CMakeLists.txt
requires CMake v2.8 at the least; but if you have an answer which requires a newer version (3.x ?), that would also be relevant.
It seems like you could use the following syntax:
which according to the documentation (https://cmake.org/cmake/help/v3.0/command/if.html) evaluates to
A minimum working example that replicates yours:
will print
while the following
will print
You could achieve similar results by using the followings:
or
the last one previously suggested by @usr1234567 in a comment.
So you could put the
inside the
if()
statement as a solution.