Always use multiple cores (/MP) flag with Visual Studio? - visual-studio

I'm using Visual Studio 2008 on my main build system. I've been playing with Visual Studio 2010 on another one. It appears that the tool still only wants to use one core when compiling unless you specify the /MP switch in the compiler switches (see How do I turn on multi-CPU/Core C++ compiles in the Visual Studio IDE (2008)?). I have to do this for every project. Is there a way to make VS always do this?

Create environment variable "CL" and set it to "/MP". Microsofts compiler cl.exe always prepend command line flags with this variable.
Some compiler features and options like #import aren't compatible with /MP flag. You will need to add /MP1 to projects used #import in a code. This will disable /MP for those projects.

Your can create a property sheet that all of your projects include, and set the /MP flag in that property sheet.
In Visual Studio 2010, you could put it in the Microsoft.Cpp.Win32.user property sheet, which is included in new projects by default (it has the old Visual C++ directories and other default settings defined in it). I don't know that modifying the default property sheet is really a good idea, but it's certainly an option.

Related

How to set the Output and Intermediate Directory in Visual Studio C++ projects generated by CMake, using Visual Studio Macros?

Here is the final desired result in Visual Studio 2019 for any C/C++ project:
The key part that I am looking for are the top 2 lines in this MSVC project properties dialog:
"Output Directory": $(Configuration)-$(PlatformArchitecture)\
"Intermediate Directory": $(Configuration)-$(PlatformArchitecture)\$(ProjectName)\
As you can see, both use Visual Studio $(xyz) macros to define the target paths.
The question(s) that I've been struggling with:
How can I make CMake spit out these strings instead of already expanded paths into the generated vcxproj files?
If I want this to happen to multiple (or all) generated MSVC C/C++ project files (libraries, executables, etc.), where do I set this up then? Do I need to patch template files or can this be done via commandline or configuration file settings, which then override the CMakeLists.txt internally defined values?
Anything special I need to mind / watch out for, if I want this setting for all vcxproj build targets as shown in the screenshot?
(in my case that's generally Debug+Release for both x86 and x64, so 2 Configurations and 2 Platforms in Visual Studio speak)
The other questions on SO (at least the ones I dug up) do not address this at all AFAICT as they replace one 'hardcoded' path with another using CMake (just using different CMake variables, but all expand to absolute paths before the vcxproj is produced).
At time of writing (CMake 3.20 and below), it is not possible to set the intermediate directory in Visual Studio. This was requested a long time ago, but no progress has been made. See: https://gitlab.kitware.com/cmake/cmake/-/issues/14999
Note that a single CMake build can only target one architecture, so the resulting Visual Studio solution will only ever let you pick the configuration, not the platform. Writing $(PlatformArchitecture) would work (returning 64 or 32 appropriately), but it would be pointlessly non-portable. Also, CMake gives you portable access to $(Configuration) in multi-config generators via the CMAKE_CFG_INTDIR variable.
So the best thing to do is to set CMAKE_*_OUTPUT_DIRECTORY in your top-level CMakeLists.txt.
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if (is_multi_config)
# Compute the bitness of the target, same as VS's $(PlatformArchitecture)
math(EXPR PlatformArchitecture "8 * ${CMAKE_SIZEOF_VOID_P}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
"${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}-${PlatformArchitecture}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
"${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}-${PlatformArchitecture}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
"${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}-${PlatformArchitecture}")
endif ()
This checks if the generator is multi-config (like Visual Studio, Xcode, or Ninja Multi-Config) and, if so, adjusts the output paths. This will give you the option later of switching away from Visual Studio to Ninja Multi-Config if you want faster builds.
Finally, you might want to use if (WIN32 AND is_multi_config) instead, if you only want to adjust the output paths on Windows (and not macOS or Linux).

How to determine the version of visual studio being used to build from within an msbuild file?

I want to use the compiler options described here:
http://randomascii.wordpress.com/2013/09/11/debugging-optimized-codenew-in-visual-studio-2012/
We have a base props sheet which gets included into all projects and would be the ideal place to set this option.
However, it seems that the compiler option changed name with Visual Studio 2013 with Update 3. Some developers in my department have not yet upgraded to this version, so I would like to dynamically select the correct name for this compiler option depending upon which version of visual studio is being used to build.
How can I do that?
I've seen there is a VisualStudioVersion property as described here: http://blogs.msdn.com/b/webdev/archive/2012/08/22/visual-studio-project-compatability-and-visualstudioversion.aspx
and here: Detect Visual Studio version from within MSBuild project
However, it seems that the value of that property changes depending upon the version of visual studio that was last used to edit the solution file, and it does not give the version of visual studio currently being used to build. (At least, that's how I read it, please correct me if that's wrong).
Hmm, also, I think it only gives the major visual studio version. I need to know which update version is being used too.

How to compile all CPP file without building the whole project in VS2010

Is it possible that I can just compile all CPP files under a project and without linking etc. the project?
The only way I know to do this is by specify the /c switch when you compile the code. Unfortunately, this option is not available from within the Visual Studio development environment, so you'll need to build your code on the command line by calling cl.exe directly. To make things easy on yourself, use the Visual Studio Command Prompt to do so.
Not sure if it's possible to get MSBuild to do this, the documentation is unclear whether the limitation is Visual Studio itself or if it's a limitation of MSBuild. I haven't actually tried for myself.
Alternatively, you can build individual source files from within the IDE by pressing Ctrl+F7 (at least, assuming the default C++ development settings). If that fails, it's also available as the "Compile" option located in the "Build" menu.
I'm not sure whether this will do what you need, but may be worth a try: create a project for an executable (rather than a library) and include all cpp files in it. Add a main() function that just returns zero. Set the C++ optimisation option to 'optimise references' (/OPT:REF). This may just compile all the cpp files but effectively ignore them during the link stage since none of them are referenced by the application.
You don't say why you need to do this - is it because linking takes a huge amount of time?

How to build MFC library from source with Visual Studio 2010

Visual Studio has historically always included the MFC library as source so you can build it yourself with the supplied makefile. But in Visual Studio 2010 there is no makefile for MFC. So, how can you rebuild it?
There is documentation on MS implying the makefile should be there:
http://msdn.microsoft.com/en-us/library/bs046sh0.aspx
.. so perhaps its an oversight, or perhaps they migrated it to msbuild but forgot to include the mfc msbuild project file.
If anyone has succesfully built a custom MFC based on that in VS2010 how did you build it?
Can the makefile from VS2008 be used with minimal tweaks? Or does anyone have an msbuild script for it?
I'm only interested in a statically linked library to be used with a specific app.
since in the end MFC is a library as any other, you can just create a new project in visual studio and add all MFC source files to it. Set the options to create a static library, set compilation/linker options as desired (eventually based on the 2008 makefile) and you're ready to go.

-D for visual studio

I have to compile one executable on Windows, the first time. I'm using visual studio 2003 and want the equivalent of a command line macro definition.
What's the VS equivalent of g++ -Dthismacro=1?
Update
For VS 2003, I found it this way. Right clicking on the project, I go to "Properties". From there, it's Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions
since you seem to be coming from a linux-flavor, why not use the commandline: the argument for macro definition is actually the same, except the compiler executable is cl and not g++:
cl -Dthismacro=1
although the documentation says the convention is to use /D, most (all?) arguments can be used with the - switch.
When using visual Studio, you can verify this: if you add a preprocessor symbol the way earlz suggested, this can be seen as /D"thismacro=1" option under Configuration->C/C++->Command Line
If you right click on the project, it should bring up a project configuration page. Goto "build" and then you should be able to define preprocessor symbols.. (hopefully it works the same in 2003 as 2008)

Resources