Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure cmake in a way that it generates Visual Studio 2013+ project files with /MP option

I have a cmake project with many cpp files (around 400+) and using /MP (multithreaded) compiler option speeds up the compilation significantly on CPU's with many cores.

The problem is, that everytime I regenerate the solution files using CMake, the option is disabled, resulting in compilation being very slow. I can fix that by changing option of EVERY single project (solution consist of many various projects) by hand inside of Visual Studio. However, everytime I regenerate the solution files by running CMake (for example when I git pull someone else's changes which added / removed some files) this change gets overwritten.

How can I make it persistent so that CMake always enable multithreaded compilation inside of VS projects?

like image 459
Petr Avatar asked Nov 22 '15 18:11

Petr


1 Answers

Put the following add_compile_options() at the top of your project's main CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8.12)
add_compile_options($<$<CXX_COMPILER_ID:MSVC>:/MP>)

or with older CMake versions < 2.8.12:

project(...)
if (MSVC)
    add_definitions("/MP")
endif()

If you can't or don't want to change the main CMakeLists.txt file, you could always add your flags manually to CMAKE_CXX_FLAGS cached variable e.g. by using CMake's GUI (assuming that your CMake project itself is not forcing the values of CMAKE_CXX_FLAGS).

References

  • Is Cmake set variable recursive?
  • Change default value of CMAKE_CXX_FLAGS_DEBUG and friends in CMake
  • What's the CMake syntax to set and use variables?
like image 124
Florian Avatar answered Nov 08 '22 17:11

Florian