How to use Clang compiler with MSBuild? - visual-studio

I'm trying to take a couple of projects normally compiled on Windows with Microsoft C++ and compile them with clang instead.
On the upside, there exists clang-cl.exe which is designed to be a drop-in replacement for cl.exe. However, even when I copy clang-cl.exe into the current directory as cl.exe, msbuild still in some cases calls Microsoft's cl.exe.
Is there a way to tell msbuild 'here, when executing Task CL, use this cl.exe instead of the usual one'? msbuild's command line options don't contain anything obvious in that direction.
Also, is there a way to tell it to supply or override command line parameters for cl without changing the project file?

This is easy to do from either command line or project file. The properties you need to configure are $(CLToolExe) and $(CLToolPath).
From the command line:
msbuild MyProj.vcxproj /p:CLToolExe=clang-cl.exe /p:CLToolPath=c:\whatever\path\to\the\tool
Alternatively, inside your .vcxproj file:
<PropertyGroup>
<CLToolExe>clang-cl.exe</CLToolExe>
<CLToolPath>c:\whatever\path\to\the\tool</CLToolPath>
</PropertyGroup>
If you are calling task CL directly inside your .vcxproj file, as opposed to just relying on common targets, just set corresponding parameters ToolExe and ToolPath of the CL task.

Since Visual Studio 2019 16.2, Microsoft provide an integration of MSbuild and ClangCl. So this can be achieved by:
Installing the “C++ Clang Tools for Windows” component
Choosing the "LLVM (clang-cl)” toolset in the IDE
Microsoft's blog post has more information on this: https://devblogs.microsoft.com/cppblog/clang-llvm-support-for-msbuild-projects/

I were using #seva solution for sometime, though in Visual studio version Version 16.10.1 it works for me only if the 'CL' prefix is omitted from the command line parameters. i.e.:
msbuild MyProj.vcxproj /p:ToolExe=clang-cl.exe /p:ToolPath=c:\whatever\path\to\the\tool

Related

Compiling C++ files during runtime using Visual Studio compiler

I'm trying figure out how to compile C++ code from an executable during runtime using Visual Studio compiler under Windows.
I'll be using Visual Studio IDE to build main project into an executable and use CreateProcess to compile other C++ files and create a DLL to later load/use/unload this DLL.
I understand that one way of doing this requires setting environment variables(mainly PATH, INCLUDE and LIB) and there's a .bat file called "vcvarsall.bat" which does this.
The part I'm stuck with is the argument(s) passed to this batch file. I see that first argument is the platform with some of the options being x86, amd64, arm, etc. But how do I programmatically figure out which one of these arguments I should be using considering main executable could've been built with any one of these?
You can prepare a regular solutionfor this purpose, containing one project with a single file, and use it to compile your file easily.
Now, all you need is to reame your file to the file name in the project and compile a solution with command line. Alternatively, you can also edit the project and replace the existing filename with your file name.
To do so you need to resolve the environment variable %DevEnvDir% and run the folowing command with the platform name (x64, win32 etc.) and configuration name(Release or Debug)
like this:
%DevEnvDir%\devenv.com \path\to\yoursolution.sln /ReBuild "Release|x64"

error MSB4057: The target "v8" does not exist in the project

I'm trying to build V8 as part of ArangoDB using the official build scripts and following the official Windows build instructions.
The compilation fails for all v8* targets (v8-build.bat):
msbuild All.sln /t:v8 /p:Configuration=Release /p:Platform=x64
msbuild All.sln /t:v8_libbase /p:Configuration=Release /p:Platform=x64
msbuild All.sln /t:v8_libplatform /p:Configuration=Release /p:Platform=x64
error MSB4057: The target "v8" does not exist in the project.
If I open the solution file in Visual Studio, it looks like this:
I can build v8, v8_libbase and v8_libplatform just fine in VS.
Windows 7 64bit
Visual Studio 2013 Ultimate
Cygwin 2.2.0
cmake 3.3.1
You can run
set MSBuildEmitSolution=1
msbuild All.sln /t:v8
Then search in the generated All.sln.metaproj file the exact target names (<Target Name="">) of all projects you want to build. v8 can have a name like _tools_\_gyp_\v8. After that you can build projects
msbuild All.sln /t:"_tools_\_gyp_\v8" /p:Configuration=Release /p:Platform=x64
The correct way to specify a target/project if it's in a solution folder is:
msbuild all.sln /t:PATH\TO\PROJECT
But in case of (tools) and (gyp) it's simply not possible, because msbuild can't handle parentheses in the target parameter /t.
So either remove ( ) and specify the path like tools\gyp\v8, or get rid of the solution folders entirely. If the solution is flat, /t:v8 will work.
Unfortunately, both the wrapping of folder names with brackets as well as the generation of non-flat .sln are hardcoded in gyp, which generated my all.sln. There is no switch to control whether solution folders are created or not. It will create them if the target version of Visual Studio is known to support this kind of nesting.
Workaround: Force flat solution generation in gyp, see
https://github.com/arangodb/arangodb/commit/796d2d263db6271142d954c8c99b9dec0fbe75e9
Reported errors to Microsoft/msbuild and Google/gyp:
https://github.com/Microsoft/msbuild/issues/157
https://code.google.com/p/gyp/issues/detail?id=494
#dothebart and this post helped me a lot to figure it out, thank you!
In VS2013 use
msbuild All.sln /p:Project=v8;Configuration=Release;Platform=x64

msbuild compiler %PATH%

I wish to build my solution using an alternative toolset (cl.exe, link.exe and so on...) by changing the PATH environment variable.
When I build the solution using "devenv MySolution.sln" it uses the alternative toolset correctly (calling the alternative cl.exe instead of Visual C++ cl.exe). When I build it using MSBuild, it uses the Visual C++ compiler anyway, ignoring my setting of PATH.
Is there any solution to this problem? Does anybody know how can I make MSBuild invoke the compiler by resolving %PATH% (without modifying all my project and solution files, of course).
According to the VCBuild Task Reference you should be able to set the ToolPath parameter to the desired path of your alternative toolset.
Admittedly this isn't as comfortable as setting the PATH environment variable and I didn't try it since I don't have VC++ at hand right now.*

Adding C# preprocesor's directive in command-line building

For C++ projects I can do this:
SET CL=/DMYDIRECTIVE
devenv.exe MySolution.sln /rebuild
What is the C# version (visual studio 2008) of this trick?
Go to the Build tab in Project Properties
On the command line, csc /define:MYDIRECTIVE
The following command lists you the options of the C# compiler
csc.exe /?
The respective option is
/define:<symbol list> Define conditional compilation symbol(s) (Short form: /d)
However, for command-line building a C# project you might prefer MSBuild. With MSBuild, it is probably easiest to create a configuration using Visual Studio's Configuration Manager that already defines appropriate conditional compilation symbols. You can then select one of the configurations from the MSBuild command line:
MSBuild MySolution.sln /t:Rebuild /p:Configuration=ReleaseWithMyDirective

nmake, visualstudio, and .mak files

I was given a C++ project that was compiled using MS Visual Studio .net 2003 C++ compiler, and a .mak file that was used to compile it. I am able to build it from the command line using nmake project.mak, but the compiler complains that afxres.h was not found. I did a little searching around and the afxres.h is in the Visual Studio directory in an includes file. Where am I supposed to specify to nmake where to look for this header file?
There should be an icon in your Start menu under Programs that opens a cmd.exe instance with all the correct MSVS environment variables set up for command line building.
Another option is running the appropriate vars batch file from a regular command prompt. The name and location varies from version to version. For VS2003, I believe it's
C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\vsvars32.bat

Resources