Is it possible, in the same CMakeLists.txt, to configure projects with / MT and others using / MD?

I have a project for which I am creating a static and dynamic version of libraries. The tools are tied to the static version; therefore, to run them on the final system, special DLLs are not required.

I can configure everything I need to compile with / MD or /MT (and corresponding debugging) with a simple set in the CMakeLists.txt root file.

For example, for force / MT, I can use the following:

 set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT" ) set ( CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT" ) set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_RELEASE} /MTd" ) set ( CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_RELEASE} /MTd" ) 

However, this means that dynamic libraries are compiled with /MT , which is not true. Is it possible to do the same thing for each project? In the end, once the solution is created, I can edit each project and fix the /MD and /MT objects according to what I need. Can it do this? That would be convenient.

I looked at set_target_properties() , but does not seem to accept CMAKE_C_FLAGS_<type> variables, and if I just set the standard set of flags, it would not be specific to Debug or Release.

The following sets the property, but I have no choice for debug and release options.

 set_target_properties( ${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "/MT" ) 

Any solution?

+3
static dll visual-studio-2012 cmake
Aug 05 '13 at 18:37
source share
1 answer

Well! I will earn!

I found this question that has a terrible solution, splitting the library in two directories and has a set () in each directory. It will work, but it will be quite a lot of work.

How to set specific compiler flags for a specific purpose in a specific build configuration using CMake?

This solution had a comment referring to this problem:

http://public.kitware.com/Bug/view.php?id=6493

which was actually just marked as corrected on 2013-06-03 12:52! This means that the solution is NOT available in the latest stable version of cmake. However, what Brad King and Stephen Kelly worked on finally works well. It can be downloaded from the daily builds found here:

http://www.cmake.org/files/dev/?C=M;O=D

The way to use the new command is a bit complicated, there is what I wrote:

 function(StaticCompile) target_compile_options( ${PROJECT_NAME} PUBLIC "/MT$<$<STREQUAL:$<CONFIGURATION>,Debug>:d>" ) endfunction() 

which in English means: if the string "$ <CONFIGURATION>" is equal to "Debug", then print "d" after "/ MT", otherwise do not print anything.

Then, anywhere, I have a goal that needs to be compiled with / MT or / MTd. I use the command as in:

 project(wpkg) add_executable( ${PROJECT_NAME} wpkg.cpp license.cpp ) StaticCompile() 

The result will be the same as expected, without any directory or other tricks!

It worked for me with the version of cmake-2.8.11.20130803-gd5dc2-win32-x86.exe, which is available today. Very cool! 8 -)

+4
Aug 05
source share



All Articles