I want to to create a VC++ project with C#. I see this MSDN article about creating C# and VB console application projects
. But how to do with VC++ project?
I had exactly the same issue as below thread. And its solution is to manipulate the raw XML, which is miserable... Is there some kind of API that I can work with?
How can an Empty Visual C++ project be created programmatically?
And besides creating a .vcxproj
fie. I want to programmatically create a solution .sln
file. Because my codebase are separated into many projects.
OK. I solved this problem by manipulating the *.sln
and *.vcxproj
files directly.
Details:
For *.vcxproj
file, I created the following template:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
__SM_C_FILES__
</ItemGroup>
<ItemGroup>
__SM_H_FILES__
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>__SM_PROJECT_GUID__</ProjectGuid>
<RootNamespace>__SM_PROJECT_ROOTNS__</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>__SM_ADDITIONAL_INCLUDE_DIRS_DEBUG__</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>__SM_ADDITIONAL_INCLUDE_DIRS_RELEASE__</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
The __SM_C_FILES__
contains sub-template like this:
<ClCompile Include="__SM_C_FILE__" />
The __SM_H_FILES__
contains sub-template like this:
<ClInclude Include="__SM_H_FILE__" />
For *.sln
file, I created a template as below:
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.40629.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "__SM_PROJ_NAME__", "__SM_PROJ_FILE_PATH__", "__SM_PROJ_GUID__"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{__SM_PROJ_GUID__}.Debug|Win32.ActiveCfg = Debug|Win32
{__SM_PROJ_GUID__}.Debug|Win32.Build.0 = Debug|Win32
{__SM_PROJ_GUID__}.Release|Win32.ActiveCfg = Release|Win32
{__SM_PROJ_GUID__}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Note the __SM_*__
parts, I replace them with the project-specific
content using the plain string replacement, which is pretty straight-forward.
And in the *.sln
file, if you have multiple projects, you may need to generated multiple parts of the Project
sections.
The {8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}
is a well-known GUID that represents the VC++
project type. A comprehensive list can be found here.
Thus, I avoided manipulating the XML.
Unlike this similar thread, I didn't create the *.vcxproj.filters
file. Though it looks a bit ugly, but I can live with that for now.
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