[CMake] Specify compiler flags as list?

Dan Kegel dank at kegel.com
Tue Oct 13 00:04:37 EDT 2015


On Mon, Oct 12, 2015 at 8:22 PM, Matthew S Wallace
<mwallace at ccmtrading.com> wrote:
> Consider the following CMakeLists.txt (hello.cpp is some standard hello world program)
>
> cmake_minimum_required (VERSION 2.6)
> project(HelloWorld)
> add_executable(hello hello.cpp)
> set(WARNING_FLAGS -Wno-char-subscripts -Wno-unused-local-typedefs)
> #set(WARNING_FLAGS "-Wno-char-subscripts -Wno-unused-local-typedefs")
> set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${WARNING_FLAGS}”)
>
> When I try to run “cmake . -DCMAKE_BUILD_TYPE=RELEASE && make” - make fails
> because there
> are semi-colons in the command line.  According to the FAQ
> (https://cmake.org/Wiki/CMake_FAQ#Why_do_I_have_unwanted_semicolons_.3B_in_my_compiler_flags.3F)
> putting the list variable in quotes should cause it to be expanded with
> spaces, but this doesn’t seem to occur.
>
> If I replace the set(WARNING_FLAGS ..) with the commented out line,
> everything works fine, but I’d rather not do this because it seems cleaner
> to use lists for compiler options (can put them on multiple lines, append to
> them, etc).  What’s the right way to do this?

Add the dang quotes, or if you prefer, translate the semicolons to
spaces later, e.g.
STRING(REPLACE ";" " " WARNING_FLAGS "${WARNING_FLAGS}")

After a while, you start to realize that you really, really need to
pay attention to how cmake represents things.

Fun fact: when using pkgconfig with cmake, you get to play games like
this all day long, e.g.

    # Grab flags from foo.pc
    PKG_CHECK_MODULES(foo REQUIRED foo)
    # Repair damage caused by spaces in library names (thanks, Mac OS
X naming conventions)
    STRING(REPLACE "-framework;Chromium;Embedded;Framework"
"-framework 'Chromium Embedded Framework'" foo_LDFLAGS
"${foo_LDFLAGS}")
    # Convert list to string because otherwise cmake thinks link
options are libraries, or something
    STRING(REPLACE ";" " " foo_LDFLAGS "${foo_LDFLAGS}")

- Dan


More information about the CMake mailing list