I am working in an environment where Visual Studio projects are often rebuild by build scripts, so even though I know how to manually change CompileAs option for Visual Studio, I am interested in making the project compile as TC from the get go.
The projects are compiled as TP, even though no cpp file is used (only c and h files are used).
The msbuild commands used (for Visual Studio 2019) are in the form msbuild name.vcxproj /t:build /p:Platform=Win32
. I have tried to append '/p:CompileAs=CompileAsC' to those commands, and the builds succeded, but they were still compiled as TP.
How to change these msbuild commands to make the Visual Studio 2019 projects have CompileAs option valued to TC?
You can't change CompileAs
in the project file from the MSBuild command line.
MSBuild is a build engine and the project and solution files are inputs to the build engine. MSBuild doesn't create or modify these files. (Effectively they are source files. The C/C++ compiler doesn't change .cpp files and MSBuild doesn't change .vcxproj files.)
MSBuild has the concepts of properties (scalar values) and items (collections). For a specific execution of MSBuild, the value of a property can be overridden from the command line.
C# and C++ project files are structured differently and options to the respective compilers are handled differently.
In a C# project, compiler options are MSBuild properties and all the C# source files are passed to the compiler together with one set of options.
But in a C++ project, the choice was made to handle compiler options as metadata on each source file. This adds both greater flexibility and greater complexity. It also means that a compiler option can't be overridden from the command line -- at least not directly.
From the question I'm unclear on the objective. If the projects should always use CompileAs=CompileAsC
then all the projects should be changed to that setting.
If the projects sometimes need to be built as /TP
and sometimes as /TC
then custom code can be added to the projects to "fix up" the metadata. The fix up code can be enabled by a property.
Example:
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
...
<CompileAs>CompileAsCpp</CompileAs>
</ClCompile>
<Link>
...
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
...
<CompileAs>CompileAsCpp</CompileAs>
</ClCompile>
<Link>
...
</Link>
</ItemDefinitionGroup>
...
<!--
After the standard settings, modify the CompileAs metadata value
if the CompileAs property is not empty.
-->
<ItemDefinitionGroup Condition="'$(Platform)'=='Win32' And 'CompileAs' != ''">
<ClCompile>
<CompileAs>$(CompileAs)</CompileAs>
</ClCompile>
</ItemDefinitionGroup>
I'm using CompileAs
as the property name which I can do because property names and metadata value names don't conflict. It may make the example a bit confusing but the upside is that the command line property option from the question, /p:CompileAs=CompileAsC
, will work.