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

Williams, Norman K norman-k-williams at uiowa.edu
Tue Sep 17 10:25:32 EDT 2013


It occurs to me that if your project requires C++11, you need something
more flexible and intelligent. I don't remember what mailing list I saw it
on (ITK?) but there is some discussion about detecting C++-11 and what the
most portable way to do that is.

You can check for specific compilers and set appropriate flags, and run
test-compile probes to determine if they're accepted.
A good exemplar to look at is in ITK:

http://bit.ly/1a0veeb ITKSetStandardCompilerFlags.cmake
http://bit.ly/16B6p8C ITK_CheckCCompilerFlag.cmake


http://bit.ly/16B6vgu ITK_CheckCCompilerFlag.cmake

Since ITK is an open source project, you can just copy and use these;
there are macros in there you can call to check if particular compiler
flags to use.

--
Kent Williams norman-k-williams at uiowa.edu






On 9/17/13 4:59 AM, "Cristian Bidea" <cristi at king.com> wrote:

>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
>
>
>--
>
>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



________________________________
Notice: This UI Health Care e-mail (including attachments) is covered by the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is confidential and may be legally privileged.  If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, or copying of this communication is strictly prohibited.  Please reply to the sender that you have received the message in error, then delete it.  Thank you.
________________________________


More information about the CMake mailing list