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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With