推荐的 Visual Studio 2026 Insider C++ 程序项目属性配置
Visual Studio 2026 来了(Visual Studio 简称 VS),虽然目前还是 VS 2026 Insider,但相信已有不少小伙伴用上了,这里给出我个人推荐的 Visual Studio 2026 Insider C++ 程序项目属性配置,也算是对自己所有 C++ 项目的一个统一。
本文中推荐的 Visual Studio 2026 Insider C++ 程序项目属性配置仅对提及的部分进行修改,其余部分请保留新建 C++ 项目后的模板初始值(不是默认值)。
一、配置属性->常规
首先,新建或者打开一个 C++ 项目,在菜单栏中找到“项目”,在二级菜单的最下方找到 XXX 和属性,其中 XXX 是你的项目名。
在新建 C++ 项目后,配置属性 -> 常规中的 Windows SDK 版本和 C++ 语言标准会被加粗,加粗意味着使用的不是默认值(或者说值被修改过)。C++ 模板的初始值绝大部分等同于默认值,但二者是不同的概念。虽然部分值实际上写的就是默认值,但它们还是被加粗了,不知道是不是 bug。
此时,笔者建议先将配置和平台切换至所有配置和所有平台,将配置属性 -> 常规中除平台工具集以外的值设置为“从父级或项目默认设置继承”(点击值右侧的小箭头从下拉列表中选择“从父级或项目默认设置继承”),将平台工具集设置为 Visual Studio v18 (v145),点击“应用”,变成如下图所示的配置。可以看到,只有平台工具集的值被加粗。
笔者也曾考虑过将所有编译生成的可执行程序放置在一个和 XXX/XXX
平级的目录中(XXX 是项目名),并在此目录中仅允许包含 x64
和 Win32
两个文件夹,例如,将所有编译生成的 Poker.exe
都放到 Poker/outputs/Win32
和 Poker/outputs/x64
下(Poker.cpp
位于 Poker/Poker
中)。但是 VS 那样设置路径是有一定道理的(让 Win32 的两个和 x64 目录平级),因为 Win32 的程序可以在 x86 和 x64 的平台上跑,而 x64 的可执行程序无法仅能在 x64 的平台上跑。因此,笔者不考虑对输出目录做出修改。
二、警告等级(可选)
笔者一般使用 /W4 来规范自己的代码,有需要的小伙伴可在 配置属性 -> C/C++ -> 常规中将警告等级调为 /W4,至于加粗的 SDL 检查字段的值,可以忽略不管。
三、启用静态编译
如果你发现在你电脑上生成的 .exe 无法在朋友的电脑上运行,那么你就需要启用静态编译来帮助你啦!当然,静态编译后的程序的体积会更大,因为它把很多的一些库和符号也一起“嵌”进了可执行程序里面。
首先,将配置和平台切换至 Debug 和所有平台,在配置属性 -> C/C++ -> 代码生成中将运行库设置为多线程调试(/MTd),点击“应用”。
随后,将配置和平台切换至 Release 和所有平台,在配置属性 -> C/C++ -> 代码生成中将运行库设置为多线程(/MT),点击“应用”。至于加粗的启用函数级链接的值,可以忽略不管,这是新建 C++ 项目中 Release 与 Debug 不同配置中的一点。
点击“确定”,返回 Visual Studio 主界面。
四、批生成
如果一个一个配置和平台去遍历,那么对四个配置和平台的组合进行生成操作起来会比较繁琐(像我这种记忆力差的生成完了又会去想是不是哪个漏了生成)。
在菜单栏找到“生成”,在二级菜单中找到“批生成…”。
批生成窗口打开后,单击“全选”,随后单击“生成”即可。
五、直接查看和修改项目属性的代码
在 VS 的 C++ 项目的目录下,有一个拓展名为 .vcxproj
的文件,使用记事本打开此文件,可以看到刚才做出的修改。由于 VS 官方并没有提供修改默认模板配置的入口,也没有提供导入和导出(可能是兼容性问题),笔者在新建不同的 C++ 项目时,都需要调一次配置,于是想出了以下办法。
找到第一个 Import Project
所在的行和最后一个 </ItemDefinitionGroup>
所在的行,将这两行以及其中间的行不删除缩进提取出来如下。
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /><PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"><UseDebugLibraries>true</UseDebugLibraries><PlatformToolset>v145</PlatformToolset><CharacterSet>Unicode</CharacterSet></PropertyGroup><PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"><UseDebugLibraries>false</UseDebugLibraries><PlatformToolset>v145</PlatformToolset><WholeProgramOptimization>true</WholeProgramOptimization><CharacterSet>Unicode</CharacterSet></PropertyGroup><PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"><UseDebugLibraries>true</UseDebugLibraries><PlatformToolset>v145</PlatformToolset><CharacterSet>Unicode</CharacterSet></PropertyGroup><PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"><UseDebugLibraries>false</UseDebugLibraries><PlatformToolset>v145</PlatformToolset><WholeProgramOptimization>true</WholeProgramOptimization><CharacterSet>Unicode</CharacterSet></PropertyGroup><Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /><ImportGroup Label="ExtensionSettings"></ImportGroup><ImportGroup Label="Shared"></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><ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"><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|x64'"><Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /></ImportGroup><PropertyGroup Label="UserMacros" /><ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"><ClCompile><WarningLevel>Level4</WarningLevel><SDLCheck>true</SDLCheck><PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions><ConformanceMode>true</ConformanceMode><RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary></ClCompile><Link><SubSystem>Console</SubSystem><GenerateDebugInformation>true</GenerateDebugInformation></Link></ItemDefinitionGroup><ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"><ClCompile><WarningLevel>Level4</WarningLevel><FunctionLevelLinking>true</FunctionLevelLinking><IntrinsicFunctions>true</IntrinsicFunctions><SDLCheck>true</SDLCheck><PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions><ConformanceMode>true</ConformanceMode><RuntimeLibrary>MultiThreaded</RuntimeLibrary></ClCompile><Link><SubSystem>Console</SubSystem><GenerateDebugInformation>true</GenerateDebugInformation></Link></ItemDefinitionGroup><ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"><ClCompile><WarningLevel>Level4</WarningLevel><SDLCheck>true</SDLCheck><PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions><ConformanceMode>true</ConformanceMode><RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary></ClCompile><Link><SubSystem>Console</SubSystem><GenerateDebugInformation>true</GenerateDebugInformation></Link></ItemDefinitionGroup><ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"><ClCompile><WarningLevel>Level4</WarningLevel><FunctionLevelLinking>true</FunctionLevelLinking><IntrinsicFunctions>true</IntrinsicFunctions><SDLCheck>true</SDLCheck><PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions><ConformanceMode>true</ConformanceMode><RuntimeLibrary>MultiThreaded</RuntimeLibrary></ClCompile><Link><SubSystem>Console</SubSystem><GenerateDebugInformation>true</GenerateDebugInformation></Link></ItemDefinitionGroup>
在未来新建 VS C++ 项目的时候,关闭 VS,直接在相应的行中进行替换,再打开 VS 即可。
本教程仅以 VS 2026 Insider 为例展开,其它 VS 版本可执行类似的操作,在配置属性 -> 常规中将 Windows SDK 版本和平台工具集保持最新即可,C++ 语言标准个人还是喜欢 ISO C++ 14 标准(能选的最低标准)来尽可能兼容更多的编译器 (其实是觉得 C++ 17 及以上 switch 直下会警告是个很傻的事情)。