c++ parallel build behavior in visual studio

967 Views Asked by At

I observe that a C++ visual studio (VS) 2017 solution with multiple projects that have multiple source files may not be built in parallel within a single project. On the other hand, the projects are always built in parallel. Particularly, I also found that when a VS solution is generated from CMake, the source files inside one project will most likely not be built in parallel when there are more than one project in the solution. From a few StackOverflow questions and MSDN, I can see that we can set up the parallel build in VS, but it does not control whether VS will build the source files within one project in parallel as well. Can someone explain to me how I can set it up so that VS will always build the source files in one project in parallel under all situations? Or is it possible to do it?

One of my typical VS solutions contains ~100 projects. Each project contains 2~30 .cpp/.h files.

EDIT:

My problem is different from Parallel compilation for projects with dependencies on each other in Visual studio. I'm wondering whether there is a way to control the behavior of parallel building within a single project when multiple projects exist in a solution. On the other hand, the cited question is related to the problem of parallel building of projects with dependencies within a solution.

EDIT2 (Answer to my question):

After a long time of investigating, I finally figure out what is causing the problem of the solution not being able to be built in parallel. As I mentioned above, my problem is not related to some parallel-build-related switches (such as /MP, # threads to use and etc.) not enabled. For those who encounters the same problem, I will explain it here. The reason turns out to be very simple. I was using a visual studio environmental variable in the project configuration. The variable I was using is %(Filename). Before executing the cl.exe command, visual studio will translate all those variables into their actual values. As I result, each source file will have a different compile option though they all have the same option "%(Filename)" in the property sheet. If two source files in the same project do not have the same compile option, they will not be built in parallel. In conclusion, use environmental variables carefully to eliminate the differences in compile options as much as possible.

1

There are 1 best solutions below

0
On

Well, It depends on the so-called physical dependency between your source files. for example if file A includes file B there is no way to compile them in parallel. It is likely that source files in different projects wont include each other, therefore it's possible to build several projects in parallel. Keep in mind that slow build is one of the characteristics of C++ and extra care should be taken in order to achieve fast build with comes down to managing physical dependency between source files for example use forward declaration. There is a deep discussion about this issue in the book 'Large-Scale C++ Software Design' by John Lakos.