Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile with /MT instead of /MD using CMake

Tags:

cmake

I'm using CMake on windows with the Windows SDK and NMake Makefiles.

By default it compiles with the /MD compiler switch.

How can I change it to compile with the /MT switch instead?

like image 373
Josh Avatar asked Jan 05 '13 14:01

Josh


People also ask

Can you compile with CMake?

Once you have edited the CMakeCache. txt file you rerun cmake, repeat this process until you are happy with the cache settings. The type make and your project should compile. Some projects will have install targets as well so you can type make install to install them.

Can I use CMake instead of make?

So, what is the real difference? CMake is much more high-level. It's tailored to compile C++, for which you write much less build code, but can be also used for general purpose build. make has some built-in C/C++ rules as well, but they are useless at best.

How does CMake choose compiler?

CMake does check for the compiler ids by compiling special C/C++ files. So no need to manually include from Module/Compiler or Module/Platform . This will be automatically done by CMake based on its compiler and platform checks.

Which compiler is used by CMake?

CMAKE_C_COMPILER holds the name of the compiler. In general cmake uses this script to find the compiler.


1 Answers

You can modify the CMAKE_CXX_FLAGS_<Build Type> and/or CMAKE_C_FLAGS_<Build Type> variables:

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

If your CMake flags already contain /MD, you can ensure that the above commands are executed after the point at which /MD is inserted (the later addition of /MT overrides the conflicting existing option), or you can set the flags from scratch:

set(CMAKE_CXX_FLAGS_RELEASE "/MT") set(CMAKE_CXX_FLAGS_DEBUG "/MTd") 

Or alternatively, you could replace the existing /MD and /MDd values with /MT and /MTd respectively by doing something like:

set(CompilerFlags         CMAKE_CXX_FLAGS         CMAKE_CXX_FLAGS_DEBUG         CMAKE_CXX_FLAGS_RELEASE         CMAKE_C_FLAGS         CMAKE_C_FLAGS_DEBUG         CMAKE_C_FLAGS_RELEASE         ) foreach(CompilerFlag ${CompilerFlags})   string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}") endforeach() 
like image 80
Fraser Avatar answered Sep 22 '22 14:09

Fraser