[CMake] How to set different compiler flags for two projects in one solution

Tanguy Krotoff tkrotoff at gmail.com
Wed Sep 30 12:18:04 EDT 2009


On Wed, Sep 30, 2009 at 6:02 PM, Tyler Roscoe <tyler at cryptio.net> wrote:
> Wow, nice bump.

:)
have searched for my problem, found this thread. That's why sometimes
web forums are more suitable.

> On Wed, Sep 30, 2009 at 05:58:50PM +0200, Tanguy Krotoff wrote:
>> I have several projects and I want to statically link one of them with
>> the Visual C++ libraries.
>> i.e I want to replace the Visual C++ /MD flag by /MT only in this
>> specific projet, not the others.
>>
>> > Can you do something like this:
>> >
>> > set (CMAKE_CXX_FLAGS_ORIG CMAKE_CXX_FLAGS)
>> > string (REPLACE "foo" "bar" ${CMAKE_CXX_FLAGS} CMAKE_CXX_FLAGS)
>> > add_library (baz ${src_files})
>> > set (CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_ORIG)
>> >
>> > ?
>>
>> This does not work.
>
> Can you be more specific? What does your CMakeLists look like and what
> command line does the compiler end up with?


project(rimes_http_request)

# Statically link with Visual C++ libraries
if (MSVC)
	foreach (MODE "_DEBUG" "_MINSIZEREL" "_RELEASE" "_RELWITHDEBINFO")
		string(REPLACE "/MD" "/MT" TMP "${CMAKE_C_FLAGS${MODE}}")
		set(CMAKE_C_FLAGS${MODE} "${TMP}" CACHE STRING "" FORCE)
		string(REPLACE "/MD" "/MT" TMP "${CMAKE_CXX_FLAGS${MODE}}")
		set(CMAKE_CXX_FLAGS${MODE} "${TMP}" CACHE STRING "" FORCE)
	endforeach (MODE)
endif (MSVC)
##

add_library(rimes_http_request SHARED rimes_http_request.cpp)

# Revert to standard CMake Visual C++ flags
# This does not work, command line for rimes_http_request still
contains /MD instead of /MT
# If I remove this part, all my projects get compiled with flag /MT
if (MSVC)
	foreach (MODE "_DEBUG" "_MINSIZEREL" "_RELEASE" "_RELWITHDEBINFO")
		string(REPLACE "/MT" "/MD" TMP "${CMAKE_C_FLAGS${MODE}}")
		set(CMAKE_C_FLAGS${MODE} "${TMP}" CACHE STRING "" FORCE)
		string(REPLACE "/MT" "/MD" TMP "${CMAKE_CXX_FLAGS${MODE}}")
		set(CMAKE_CXX_FLAGS${MODE} "${TMP}" CACHE STRING "" FORCE)
	endforeach (MODE)
endif (MSVC)
##

target_link_libraries(rimes_http_request
	...
)

install(TARGETS rimes_http_request ${INSTALL_TARGETS_DEFAULT_ARGS})


More information about the CMake mailing list