8

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?

Petr
  • 13,747
  • 20
  • 89
  • 144

1 Answers1

12

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

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
  • Which version introduced this option? I am now getting errors on debian like "unknown option add_compile_options" with ancient CMake version. Is it possible to wrap it somehow so that it's ignored on old cmake versions? – Petr Nov 23 '15 at 10:14
  • @Petr If you want to support older versions also, I would recommend to not take `add_compile_options()` but a command that even older versions do understand. I've updated the answer accordingly. – Florian Nov 23 '15 at 10:34