简体   繁体   中英

CMake - appending CMAKE_CXX_FLAGS from the command line

I would like to add some CMAKE_CXX_FLAGS from the command line to the flags defined in CMakeLists.txt, for example:

cmake -DCMAKE_CXX_FLAGS="-Wall" ..

It will work only when flags are appended to the list in the CMakeLists.txt, for example:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

In the following case:

set(CMAKE_CXX_FLAGS "-Wall")

CMAKE_CXX_FLAGS passed from the command line wil be overwritten.

Is there any way to prevent overwritting flags defined in the command line without modyfing CMakeLists.txt ?

You can use APPEND subcommand.:

list(APPEND <list> [<element> ...])

Appends elements to the list. If no variable named exists in the current scope its value is treated as empty and the elements are appended to that empty list.[documentation]

So something like this should work:

list(APPEND CMAKE_CXX_FLAGS "-Wall")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM