[CMake] Set CMAKE_<LANG>_FLAGS_* and overriding

Cristian Bidea cristi at king.com
Tue Sep 17 05:59:01 EDT 2013


Hi David

You have a number of options here:

1. You can pass the additional compile flag trough another variable 
instead of trying to override CMAKE_CXX_FLAGS.

cmake .. -DFREEBSD_FLAG="-stdlib=libc++"

... then in CMakeLists.txt you can set the compile flags as follows:

set (CMAKE_CXX_FLAGS "-std=c++11 ${FREEBSD}")

When the FREBSD option isn't set, then the string would contain just 
"-std=c++11 ".

2. Another way would be to set compile flags depending on the platform 
you're compiling on (that works if you don't do cross compiling):

if ("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
    set (CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++")
else ()
    set (CMAKE_CXX_FLAGS "-std=c++11")
endif ()

3. And the third option would be to also preserve the previous value of 
CMAKE_CXX_FLAGS

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

... so now it doesn't get overriden!

Hope this helps!

-Cristi

On Tue Sep 17 12:40:31 2013, David Demelier wrote:
> Hello,
>
> I'm writing a C++11 project. To enable C++11 it requires some switches,
> such as -std=c++11.
>
> So for the moment, I have in my CMakeLists.txt:
>
> set(CMAKE_CXX_FLAGS "-std=c++11")
>
> So with that, I'm sure that any build configuration will add this.
> However, with clang++, you need to pass an additional flag (for FreeBSD
> especially): -stdlib=libc++.
>
> So I would like to be able to do at command line:
>
> cmake .. -DCMAKE_CXX_FLAGS="-std=c++11 -stdlib=libc++"
>
> But with that, it just ignore my setting and take the one from the
> CMakeLists.txt
>
> What is the correct way to:
>
> * Set CXX flags globally, for any type of configuration
> * Let them be overriden?
>
> Regards,
>
> David.
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake




More information about the CMake mailing list