[CMake] CMAKE_CXX_STANDARD_LIBRARIES

Philip Lowman philip at yhbt.com
Mon May 12 21:37:06 EDT 2008


On Mon, May 12, 2008 at 2:46 PM, Glenn Pierce <glennpierce at gmail.com> wrote:

> Hi I have a library that I compile with visual studio and I would like to
> make sure it is compiled with the option of the runtime library set too
> multi-threaded /MT.
>
> Also I would like it only linked to "user32.lib gdi32.lib"
>
> i have tried using
>
> SET(CMAKE_CXX_STANDARD_LIBRARIES, "user32.lib gdi32.lib")
>
> but it seems to have no effect.
> I don't know what to set to get the runtime library to be multi-threaded
> /MT.
>
> Does anyone have any ideas ?**


I know of two ways to do this but both have drawbacks (hopefully someone who
understands the innards of CMake better can suggest a better alternative):

1. Use the FORCE option to force the cache variables to be the way you want
them to.  This will probably work for your setup, it does have the minor
drawback of not allowing your users to change the compile flags from within
CMake's cache editor without modifying the CMakeLists.txt file.

PROJECT(foo)
IF(MSVC)
    SET(CMAKE_C_FLAGS_DEBUG "/D_DEBUG /MTd /Zi /Ob0 /Od /GZ" CACHE STRING
        "Flags used by the compiler during debug builds." FORCE)
    SET(CMAKE_CXX_FLAGS_DEBUG "/D_DEBUG /MTd /Zi /Ob0 /Od /GZ" CACHE STRING
        "Flags used by the compiler during debug builds." FORCE)
    SET(CMAKE_C_FLAGS_MINSIZEREL "/MT /O1 /Ob1 /D NDEBUG" CACHE STRING
        "Flags used by the compiler during release minsize builds." FORCE)
    SET(CMAKE_CXX_FLAGS_MINSIZEREL "/MT /O1 /Ob1 /D NDEBUG" CACHE STRING
        "Flags used by the compiler during release minsize builds." FORCE)
    SET(CMAKE_C_FLAGS_RELEASE "/MT /O2 /Ob2 /D NDEBUG" CACHE STRING
        "Flags used by the compiler during release builds (/MD /Ob1 /Oi /Ot
/Oy /Gs will produce slightly less optimized but smaller files)." FORCE)
    SET(CMAKE_CXX_FLAGS_RELEASE "/MT /O2 /Ob2 /D NDEBUG" CACHE STRING
        "Flags used by the compiler during release builds (/MD /Ob1 /Oi /Ot
/Oy /Gs will produce slightly less optimized but smaller files)." FORCE)
    SET(CMAKE_C_FLAGS_RELWITHDEBINFO "/MT /Zi /O2 /Ob1 /D NDEBUG" CACHE
STRING
        "Flags used by the compiler during Release with Debug Info builds."
FORCE)
    SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/MT /Zi /O2 /Ob1 /D NDEBUG" CACHE
STRING
        "Flags used by the compiler during Release with Debug Info builds."
FORCE)
ENDIF()


2. I'm not sure if this is recommended but I discovered you can set
CMAKE_NOT_USING_CONFIG_FLAGS to 1 prior to calling PROJECT().  This will let
you define the default compiler options you want to use and users will be
able to change them in the cache later but I haven't been able to find a way
to conditionally assign the variables so you can use more than one compiler.
In other words, you could assign them for Visual Studio's CL compiler but
the flags would never work if you went to use GCC to compile your project.

I recommend something similar to the former option.

-- 
Philip Lowman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20080512/fa4eacb0/attachment.htm>


More information about the CMake mailing list