使用项目属性Working with Project Properties

在 IDE 中,生成项目的需要的全部信息都公开为属性。In the IDE, all information that is needed to build a project is exposed as properties. 此信息包括应用程序名称、扩展名(如 DLL、LIB、EXE)、编译器选项、链接器选项、调试器设置、自定义生成步骤和许多其他操作。This information includes the application name, extension (such as DLL, LIB, EXE), compiler options, linker options, debugger settings, custom build steps, and many other things. 通常情况下,使用属性页(“项目”|“属性”)来查看和修改这些属性。Typically, you use property pages ( Project | Properties) to view and modify these properties.

创建项目时,系统分配各种属性的值。When you create a project, the system assigns values for various properties. 根据项目类型和在应用向导中所选的选项类型,默认值会有所不同。The defaults vary somewhat depending on the kind of project and what options you choose in the app wizard. 例如,ATL 项目具有与 MIDL 文件相关的属性,但这些属性在基本控制台应用程序中都不存在。For example, an ATL project has properties related to MIDL files, but these are absent in a basic console application. 默认属性在属性页的“常规”窗格中显示:The default properties are shown in the General pane in the Property Pages:

Visual C++项目默认值Visual C++ Project Defaults

某些属性(例如应用程序名称)适用于所有生成变量,而不考虑目标平台或其为调试版本还是发布版本。Some properties, such as the application name, apply to all build variations, regardless of the target platform or whether it is a debug or release build. 但是大多数属性都依赖于配置。But most properties are configuration-dependent. 这是由于编译器必须知道程序将在哪个特定平台上运行,以及使用哪个特定编译器选项以便生成正确的代码。This is because the compiler has to know what specific platform the program will run on and what specific compiler options to use in order to generate the correct code. 因此,设置属性时,请务必注意新值应适用于哪个配置和平台。Therefore, when you set a property, it is important to pay attention to which configuration and platform the new value should apply to. 它应该仅适用于调试 Win32 版本还是也适用于调试 ARM 和调试 x64 版本?Should it apply only to Debug Win32 builds, or should it also apply to Debug ARM and Debug x64? 例如,默认情况下,“优化”属性在版本配置中设为“最大化速度(/O2)”,但在调试配置中禁用。For example, the Optimization property, by default, is set to Maximize Speed (/O2) in a Release configuration, but is disabled in the Debug configuration.

这样设置属性页是便于随时可以查看并在必要时修改属性值应适用于哪个配置和平台。The property pages are designed so that you can always see, and if necessary modify, which configuration and platform a property value should apply to. 下图显示了属性页,该页的顶部列表框中包含配置和平台信息。The following illustration shows the property pages with the configuration and platform information in the list boxes at the top. 在此处设置“优化”属性时,它将仅适用于调试 Win32 版本,这正好是活动配置,如红色箭头所示。When the Optimization property is set here, it will apply only to Debug Win32 builds, which happens to be the active configuration, as shown by the red arrows.

显示活动配置的 Visual C++属性页Visual C++ Property Pages showing active configuration

下图显示相同的项目属性页,但该配置已更改为发布。The following illustration shows the same project property page, but the configuration has been changed to Release. 请注意“优化”属性的不同值。Note the different value for the Optimization property. 另请注意,活动配置仍是调试。Also note that the active configuration is still Debug. 可以在此处设置任何配置的属性;它不必处于活动状态。You can set properties for any configuration here; it doesn't have to be the active one.

显示版本配置的 Visual C++ 属性页Visual C++ Property Pages showing release config

此项目系统本身基于 MSBuild,它定义了任何类型的生成项目的文件格式和规则。The project system itself is based on MSBuild, which defines file formats and rules for building projects of any kind. MSBuild 承应生成多个配置和平台的复杂性,但你需要了解一些 MSBuild 的工作原理。MSBuild manages much of the complexity of building for multiple configurations and platforms, but you need to understand a little bit about how it works. 如果想要定义自定义配置,或者创建可共享和导入多个项目的可重用的属性集,这一点尤为重要。This is especially important if you want to define custom configurations or create reusable sets of properties that you can share and import into multiple projects.

项目属性直接存储在项目文件 (*.vcxproj) 中或项目文件导入的其他 .xml 或 .props 文件(提供有默认值)中。Project properties are stored either directly in the project file (*.vcxproj) or in other .xml or .props files that the project file imports and which supply default values. 如前面所示,相同配置的同一属性在不同文件中可能分配有不同值。As shown earlier, the same property for the same configuration may be assigned a different value in different files. 生成项目时,MSBuild 引擎以明确定义的顺序评估项目文件和所有导入的文件(如下所述)。When you build a project, the MSBuild engine evaluates the project file and all the imported files in a well-defined order (described below). 在评估每个文件时,该文件中定义的任何属性值都将覆盖现有值。As each file is evaluated, any property values defined in that file will override the existing values. 未指定的任何值都从之前已评估的文件继承。Any values that are not specified are inherited from files that were evaluated earlier. 因此,使用属性页设置属性时,也请务必注意设置它的位置。Therefore, when you set a property with property pages, it is also important to pay attention to where you set it. 如果在 .props 文件中将属性设置为“X”,但该属性在项目文件中已设为“Y”,然后将在属性设为“Y”的情况下生成项目。If you set a property to "X" in a .props file, but the property is set to "Y" in the project file, then the project will build with the property set to "Y". 如果在项目项(例如 .cpp 文件)上,相同的属性已设为“Z”,然后 MSBuild 引擎将使用“Z”值。If the same property is set to "Z" on a project item, such as a .cpp file, then the MSBuild engine will use the "Z" value. 有关更多信息,请参见本文后面的属性继承For more information, see Property inheritance later in this article.

生成配置Build configurations

配置只是给定名称的任意一组属性。A configuration is just an arbitrary group of properties that are given a name. Visual Studio 提供“调试”和“发布”配置,并且每个配置都相应地为调试版本和发布版本设置各种属性。Visual Studio provides Debug and Release configurations and each sets various properties appropriately for a debug build or release build. 可以使用 Configuration Manager 来定义自定义配置,提供一种简单的方法来组合属性获得特定风格的版本。You can use the Configuration Manager to define custom configurations as a convenient way to group properties for a specific flavor of build. 属性管理器用于处理属性的高级事项,但我们在此处介绍是因为其有助于属性配置可视化。The Property Manager is used for advanced work with properties, but we introduce it here because it helps visualize property configurations. 可以从“视图”|“属性管理器”或“视图”&#124“其他窗口”|“属性管理器”访问属性管理器,具体取决于设置。You access it from View | Property Manager or View | Other Windows | Property Manager depending on your settings. 它具有项目中每个配置/平台对的节点。It has nodes for each configuration/platform pair in the project. 这些节点的每个节点下都是属性表(.props 文件)的节点,为该配置设置某些特定属性。Under each of these nodes are nodes for property sheets (.props files) that set some specific properties for that configuration.

属性管理器Property Manager

如果转到属性页中的“常规”窗格(请参阅以上图示),并将字符集属性设为“未设置”而不是“使用 Unicode”,并单击“确定”,属性管理器将不对当前配置显示“Unicode 支持”属性表,但该表对其他配置仍然存在。If you go to the General pane in the Property Pages (see the illustration above) and set the Character Set property to "Not Set" instead of "Use Unicode" and click OK, Property Manager will show no Unicode Support property sheet for the current configuration, but it will still be there for other configurations.

有关属性管理器和属性表的详细信息,请参阅本文后面的创建可重用的属性配置For more information about Property Manager and property sheets, see Creating reusable property configurations later in this article.

提示

.user 文件是一项旧功能,我们建议将其删除以便根据配置/平台正确地对属性分组。The .user file is a legacy feature and we recommend that you delete it in order to keep properties correctly grouped according to configuration/platform.

目标平台Target platforms

目标平台是指可执行文件将在此之上运行的各种设备和/或操作系统。Target platform refers to the kind of device and/or operating system that the executable will run on. 可以生成多个平台的项目。You can build a project for more than one platform. C++ 项目的可用目标平台取决于各种项目;包括但不限于 Win32、x64、ARM、Android 和 iOS。The available target platforms for C++ projects depend on the kind of project; they include but are not limited to Win32, x64, ARM, Android, and iOS. 可能在 Configuration Manager 中看到的 x86 目标平台等同于本机 C++ 项目中的 Win32。The x86 target platform that you might see in Configuration Manager is identical to Win32 in native C++ projects. Win32 意味着 32 位的 Windows,而 x64 意味着 64 位的 Windows。Win32 means 32-bit Windows and x64 means 64-bit Windows. 有关这两个平台的详细信息,请参阅运行 32 位的应用程序For more information about these two platforms, see Running 32-bit applications.

可能在 Configuration Manager 中看到的任意 CPU 目标平台值对本机 C++ 项目无影响;而是与 C++/CLI 和其他 .NET 项目类型相关。The Any CPU target platform value that you might see in Configuration Manager has no effect on native C++ projects; it is relevant for C++/CLI and other .NET project types. 有关详细信息,请参阅 /CLRIMAGETYPE(指定 CLR 映像的类型)For more information, see /CLRIMAGETYPE (Specify Type of CLR Image).

属性页Property pages

如上文所述,Visual C++ 项目系统基于 MSBuild,并且值存储在 XML 项目文件、默认 .props 和 .targets 文件中。As stated earlier, the Visual C++ project system is based on MSBuild and the values are stored in the XML project file, default .props and .targets files. 对于 Visual Studio 2015,这些文件位于\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140。For Visual Studio 2015, these files are located in \Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140. 对于 Visual Studio 2017,这些文件位于 \Program Files (x86)\Microsoft Visual Studio\2017\edition\Common7\IDE\VC\VCTargets,其中“edition”是已安装的 Visual Studio 版本。For Visual Studio 2017, these files are located in \Program Files (x86)\Microsoft Visual Studio\2017\edition\Common7\IDE\VC\VCTargets, where edition is the Visual Studio edition installed. 属性也存储在可能想要添加到自己项目的任何自定义 .props 文件中。Properties are also stored in any custom .props files that you might add to your own project. 我们强烈建议不要手动编辑这些文件,而是使用 IDE 中的属性页来更改所有属性,特别是参与继承的属性,除非你非常了解 MSBuild。We highly recommend that you NOT edit those files manually, and instead use the property pages in the IDE to modify all properties, especially those that participate in inheritance, unless you have a very good understanding of MSBuild.

下图显示了 Visual C++ 项目的属性页。The following illustration shows the property pages for a Visual C++ project. 在左窗格中,选中“VC++ 目录”规则,右窗格中即会列出与该规则关联的属性。In the left pane, the VC++ Directories rule is selected, and the right pane lists the properties that are associated with that rule. 遗憾的是,$(...) 值称为宏。The $(...) values are unfortunately called macros. 这些不是 C/C++ 宏,而是简单的编译时常量。These are not C/C++ macros but simply compile-time constants. 本文后面的属性页宏部分中讨论了宏。Macros are discussed in the Property page macros section later in this article.)

项目属性页Project property pages

警告

已删除 Visual Studio 的早期版本中的“通用属性”配置。The Common Properties configurations in earlier versions of Visual Studio have been removed. 若要添加对项目的引用,现在以用于托管语言的相同方式使用“添加引用”对话框。To add a reference to a project, you now use the Add Reference dialog in the same way as for managed languages. 请参阅管理项目中的引用See Managing references in a project.

设置项目属性To set a property for a project

  1. 大多数情况下,可以在项目级别设置属性无需创建自定义属性表。For most scenarios, you can set properties at the project level without creating a custom property sheet. 在主菜单上选择“项目”|“属性”或右键单击解决方案资源管理器中的项目节点,并选择“属性”。On the main menu, choose Project | Properties, or right-click on the project node in Solution Explorer and choose Properties.

  2. 使用对话框顶部的“配置”和“平台”列表框,指定哪些属性组应该应用更改。Use the Configuration and Platform list boxes at the top of the dialog box to specify which property groups should apply your changes. 在许多情况下,“所有平台”和“所有配置”是正确的选择。In many cases All Platforms and All Configurations are the right choice. 若要只为某些配置设置属性,请在“属性管理器”中选择多个配置,然后打开快捷菜单并选择“属性”。To set properties for just some configurations, multi-select them in Property Manager, and then open the shortcut menu and choose Properties.

“属性页”对话框仅显示适用于当前项目的属性页。The Property Pages dialog box shows only the property pages that apply to the current project. 例如,如果该项目没有 .idl 文件则不会显示 MIDL 属性页。For example, if the project does not have an .idl file, the MIDL property page is not displayed.

在属性页中突出显示某一属性时,可以按 F1,转到参考主题,详细了解相应编译器或链接器开关。When you highlight a property in a Property Page, you can press F1 to go to the reference topic for more information about the corresponding compiler or linker switch.

可以在这些主题中找到有关每个属性页的详细信息:You can find more information about each Property Page in these topics:

快速浏览和搜索所有属性To quickly browse and search all properties

“所有选项”属性页(在“属性页”对话框中“配置属性”|“C/C++”节点下)可实现快速浏览和搜索当前上下文中可用的属性。The All Options property page (under the Configuration Properties | C/C++ node in the Property Pages dialog box) provides a quick way to browse and search the properties that are available in the current context. 它具有特殊的搜索框和简单的语法,能够帮助你筛选结果:It has a special search box and a simple syntax to help you filter results:

无前缀:No prefix:
仅在属性名称中搜索(不区分大小写的子字符串)。Search in property names only (case-insensitive substring).

'/' 或 '-':'/' or '-' :
仅在编译器开关中搜索(不区分大小写的前缀)Search only in compiler switches (case-insensitive prefix)

v:v:
仅在值中搜索(不区分大小写的子字符串)。Search only in values (case-insensitive substring).

属性页宏Property page macros

宏是编译时常量,是指由 Visual Studio 或 MSBuild 系统定义的值,或是由用户定义的值。A macro is a compile-time constant that can refer to a value that is defined by Visual Studio or the MSBuild system, or to a user-defined value. 通过使用宏(而不是硬编码值,例如目录路径),你可更轻松地在计算机之间以及 Visual Studio 的版本之间共享属性设置,并且可更好地确保项目设置正确地参与属性继承。By using macros instead of hard-coded values such as directory paths, you can more easily share property settings between machines and between versions of Visual Studio, and you can better ensure that your project settings participate correctly in property inheritance. 可以使用属性编辑器来查看所有可用宏的值。You can use the Property Editor to view the values of all available macros.

预定义宏Predefined macros

全局宏global macros
应用于项目配置的所有项目。Applies to all items in a project configuration. 具有语法 $(name)Has the syntax $(name). 全局宏的示例是 $(VCInstallDir),它存储 Visual Studio 安装的根目录。An example of a global macro is $(VCInstallDir), which stores the root directory of your Visual Studio installation. 全局宏与 MSBuild 中的 PropertyGroup 相对应。A global macro corresponds to a PropertyGroup in MSBuild.

项宏item macros
具有语法 %(name)Has the syntax %(name). 对于文件来说,仅适用于该文件的项宏,例如可以使用 %(AdditionalIncludeDirectories) 来指定仅适用于特定文件的包含目录。For a file, an item macro applies only to that file—for example, you can use %(AdditionalIncludeDirectories) to specify include directories that apply only to a particular file. 这种项宏与 MSBuild 中的 ItemGroup 元数据相对应。This kind of item macro corresponds to an ItemGroup metadata in MSBuild. 在项目配置中使用时,项宏适用于特定类型的所有文件。When it's used in the context of a project configuration, an item macro applies to all files of a certain type. 例如,C/C++“预处理器定义”配置属性可以采用适用于项目中所有 .cpp 文件的 %(PreprocessorDefinitions) 项宏。For example, the C/C++ Preprocessor Definitions configuration property can take a %(PreprocessorDefinitions) item macro that applies to all .cpp files in the project. 这种项宏与 MSBuild 中的 ItemDefinitionGroup 元数据相对应。This kind of item macro corresponds to an ItemDefinitionGroup metadata in MSBuild. 有关详细信息,请参阅项定义For more information, see Item Definitions.

用户定义的宏User-defined macros

你可以创建用户定义的宏,以便在项目生成中将宏用作变量。You can create user-defined macros to use as variables in project builds. 例如,可以创建一个用户定义的宏来提供自定义生成步骤或自定义生成工具的值。For example, you could create a user-defined macro that provides a value to a custom build step or a custom build tool. 用户定义的宏是名称/值对。A user-defined macro is a name/value pair. 在项目文件中,使用 $(name) 表示法访问该值。In a project file, use the $(name) notation to access the value.

用户定义的宏存储在属性表中。A user-defined macro is stored in a property sheet. 如果你的项目尚未包含属性表,可以按照创建可重用属性配置中的步骤创建一个。If your project does not already contain a property sheet, you can create one by following the steps under Creating reusable property configurations.

创建用户定义的宏To create a user-defined macro
  1. 在“属性管理器”窗口中(在菜单栏上,依次选择“视图”、“属性管理器”),打开属性表的快捷菜单(名称以 .user 结尾),然后选择“属性”。In the Property Manager window (on the menu bar, choose View, Property Manager), open the shortcut menu for a property sheet (its name ends in .user) and then choose Properties. 此时将打开该属性表的“属性页”对话框。The Property Pages dialog box for that property sheet opens.

  2. 在对话框的左窗格中,选择“用户宏”。In the left pane of the dialog box, select User Macros. 在右窗格中,选择“添加宏”按钮,打开“添加用户宏”对话框。In the right pane, choose the Add Macro button to open the Add User Macro dialog box.

  3. 在对话框中,指定宏的名称和值。In the dialog box, specify a name and value for the macro. 根据需要,选中“将此宏设置为生成环境中的环境变量”复选框。Optionally, select the Set this macro as an environment variable in the build environment check box.

属性编辑器Property Editor

你可以使用属性编辑器来修改特定字符串属性,选择宏作为值。You can use the Property Editor to modify certain string properties and select macros as values. 若要访问“属性编辑器”,在属性页中选择属性,然后选择右侧的向下箭头按钮。To access the Property Editor, select a property on a property page and then choose the down arrow button on the right. 如果下拉列表包含“<编辑>”,那么你可以选择它来显示该属性的属性编辑器。If the drop-down list contains <Edit>, then you can choose it to display the Property Editor for that property.

属性_编辑器_下拉列表Property_Editor_Dropdown

在属性编辑器中,可以选择“宏”按钮查看可用宏及这些宏的当前值。In the Property Editor, you can choose the Macros button to view the available macros and their current values. 下图显示选中“宏”按钮后,“附加包含目录”属性的属性编辑器。The following illustration shows the Property Editor for the Additional Include Directories property after the Macros button was chosen. 如果选中“从父级或项目默认设置继承”复选框并添加了新值,则该值会附加到当前被继承的任意值。When the Inherit from parent or project defaults check box is selected and you add a new value, it is appended to any values that are currently being inherited. 如果清除复选框,则新值会替换继承值。If you clear the check box, your new value replaces the inherited values. 在大多数情况下,选中复选框。In most cases, leave the check box selected.

属性编辑器,Visual C++Property editor, Visual C++

创建可重用的属性配置Creating reusable property configurations

虽然可以根据每个用户和每台计算机设置“全局”属性,但我们不建议这样做。Although you can set "global" properties on a per-user, per-computer basis, we no longer recommend it. 相反,建议你使用“属性管理器”创建属性表,来存储你希望能够重新使用或与其他人共享的每一类项目的设置。Instead, we recommend that you use Property Manager to create a property sheet to store the settings for each kind of project that you want to be able to reuse or share with others. 属性表还使无意中更改其他项目类型的属性设置的可能性变小。Property sheets also make it less likely that property settings for other project types will be inadvertently changed. 创建可重用的属性配置中详细讨论了属性表。Property sheets are discussed in more detail Creating reusable property configurations.

重要

.user 文件以及它们存在问题的原因.user files and why they are problematic

过去的 Visual Studio 版本使用了全局属性表,此表包含 .user 文件名扩展,并且位于<userprofile>\AppData\Local\Microsoft\MSBuild\v4.0\ folder。Past versions of Visual Studio used global property sheets that had a .user file name extension and were located in the <userprofile>\AppData\Local\Microsoft\MSBuild\v4.0\ folder. 我们不再推荐这些文件,因为它们是针对每个用户、每台计算机来设置项目配置属性的。We no longer recommend these files because they set properties for project configurations on a per-user, per-computer basis. 特别是如果你在生成计算机上面向多个平台,此类“全局”设置会影响生成。Such "global" settings can interfere with builds, especially when you are targeting more than one platform on your build computer. 例如,如果你同时拥有一个 MFC 项目和 Windows Phone 项目,则其中一个项目的 .user 属性将会无效。For example, if you have both an MFC project and Windows Phone project, the .user properties would be invalid for one of them. 可重用的属性表更为灵活,而且更加可靠。Reusable property sheets are more flexible and more robust.

尽管 Visual Studio 仍安装 .user 文件并参与属性继承,但默认情况下,这些文件为空。Although .user files are still installed by Visual Studio and participate in property inheritance, they are empty by default. 最佳做法是删除项目在“属性管理器”中的引用,以确保项目按每个计算机设置和每个用户独立运行。这对确保 SCC(源代码管理)环境中的正确行为来说非常重要。The best practice is to delete the reference to them in Property Manager to ensure that your projects operate independently of any per-user, per-computer settings This is important to ensure correct behavior in a SCC (source code control) environment.

若要显示“属性管理器”,请在菜单栏上依次选择“视图”、“其他窗口”、“属性管理器”。To display Property Manager, on the menu bar, choose View, Other Windows, Property Manager.

如果你想将经常使用的属性集应用于多个项目,则可以使用“属性管理器”在可重用的属性表文件中捕获,按照惯例,文件的扩展名为 .props。If you have a common, frequently used set of properties that you want to apply to multiple projects, you can use Property Manager to capture them in a reusable property sheet file, which by convention has a .props file name extension. 你可以将一张或多张表应用于新项目,这样就不必从零开始设置属性。You can apply the sheet (or sheets) to new projects so that you don't have to set its properties from scratch. 若要访问“属性管理器”,在菜单栏中选择“视图”、“属性管理器”。To access Property Manager, on the menu bar, choose View, Property Manager.

“属性管理器”快捷方式菜单Property Manager shortcut menu

在每个配置节点下,将看到适用于该配置的每个属性表的节点。Under each configuration node, you see nodes for each property sheet that applies to that configuration. 创建项目时,此系统添加了属性表,该表基于在应用向导中所选的选项来设置值。The system adds property sheets that set values based on options you choose in the app wizard when you create the project. 右键单击任何节点并选择“属性”来查看适用于该节点的属性。Right-click any node and choose Properties to see the properties that apply to that node. 所有属性表都自动导入到项目的“主”属性表 (ms.cpp.props) 中并进行评估,以便在属性管理器中显示。All the property sheets are imported automatically into the project's "master" property sheet (ms.cpp.props) and are evaluated in the order they appear in Property Manager. 可以移动它们来更改评估顺序。You can move them to change the evaluation order. 稍后评估的属性表将覆盖之前已评估表中的值。Property sheets that are evaluated later will override the values in previously-evaluated sheets.

如果选择“添加新项目属性表”,然后进行选择(例如 MyProps.props 属性表),将显示属性页对话框。If you choose Add New Project Property Sheet and then select, for example, the MyProps.props property sheet, a property page dialog box appears. 请注意,适用于 MyProps 属性表;你所做的任何更改都将写入表中,而非属性文件 (.vcxproj) 中。Notice that it applies to the MyProps property sheet; any changes you make are written to the sheet, not to the project file (.vcxproj).

如果直接在 .vcxproj 文件中设置同一属性,属性表中的属性将被重写。Properties in a property sheet are overridden if the same property is set directly in the .vcxproj file.

你可以按所需的频率导入属性表。You can import a property sheet as often as required. 一个解决方案中的多个项目可从同一个属性表继承设置,一个项目可有多个表。Multiple projects in a solution can inherit settings from the same property sheet, and a project can have multiple sheets. 属性表自身可以从另一个属性表继承设置。A property sheet itself can inherit settings from another property sheet.

你还可以为多个配置创建一个属性表。You can also create one property sheet for multiple configurations. 为此,请为每个配置创建属性表,打开其中一个配置的快捷菜单,选择“添加现有属性表”,然后添加其他表。To do this, create a property sheet for each configuration, open the shortcut menu for one of them, choose Add Existing Property Sheet, and then add the other sheets. 但是,如果使用一个常用属性表,请注意在设置属性时,它会获取该表适用的所有配置设置,而且 IDE 不会显示从给定属性表继承的项目或其他属性表。However, if you use one common property sheet, be aware that when you set a property, it gets set for all configurations that the sheet applies to, and that the IDE doesn’t show which projects or other property sheets are inheriting from a given property sheet.

在具有多个项目的大型解决方案中,创建解决方案级别的属性表非常有用。In large solutions that will have many projects, it can be useful to create a property sheet at the solution level. 在将项目添加到解决方案时,请使用“属性管理器”将该属性表添加到项目中。When you add a project to the solution, use Property Manager to add that property sheet to the project. 如果项目级别为必填字段,则可以添加一个新属性表以设置指定的项目值。If required at the project level, you can add a new property sheet to set project-specific values.

重要

由于 .props 文件不作为项目项创建,因此该文件默认不参与源代码管理。A .props file by default does not participate in source control because it isn’t created as a project item. 如果你希望将文件加入源代码管理,则可以手动添加文件作为解决方案项。You can manually add the file as a solution item if you want to include it in source control.

创建属性表To create a property sheet

  1. 在菜单栏上,依次选择“查看”、“属性管理器”。On the menu bar, choose View, Property Manager. 此时将打开“属性管理器”。The Property Manager opens.

  2. 若要定义属性表的范围,请选择属性表适用的项。To define the scope of the property sheet, select the item to which it applies. 这可能是一个特殊配置,或另一个属性表。This can be a particular configuration, or another property sheet. 打开该项的快捷菜单,然后选择“添加新项目属性表”。Open the shortcut menu for this item and then choose Add New Project Property Sheet. 指定一个名称和位置。Specify a name and location.

  3. 在“属性管理器”中,打开新的属性表然后设置要包括的属性。In Property Manager, open the new property sheet and then set the properties you want to include.

属性继承Property inheritance

项目属性已分层。Project properties are layered. 每层继承前一层的值,但是继承的值可以通过设置属性显式重写。Each layer inherits the values of the previous layer, but an inherited value can be overridden by setting the property explicitly. 这是基本的继承树:Here's the basic inheritance tree:

  1. 来自 MSBuild CPP 工具集的默认设置(..\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.Cpp.Default.props,由 .vcxproj 文件导入。)Default settings from the MSBuild CPP Toolset (..\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.Cpp.Default.props, which is imported by the .vcxproj file.)

  2. 属性表Property sheets

  3. .vcxproj 文件。.vcxproj file. (可能重写默认设置和属性页设置。)(Can override the default and property sheet settings.)

  4. 项元数据Items metadata

提示

在属性页中,bold 的属性在当前上下文中定义。On a property page, a property in bold is defined in the current context. 普通字体的属性将被继承。A property in normal font is inherited.

项目文件 (.vcxproj) 在生成时导入其他属性表。A project file (.vcxproj) imports other property sheets at build time. 在导入所有属性表后,对项目文件进行计算,然后所有属性值都使用最后一个定义。After all property sheets are imported, the project file is evaluated and the last definition of any property value is the one that's used. 有时,通过查看展开的文件来确定给定的属性值如何继承非常有用。Sometimes it's useful to view the expanded file to determine how a given property value is inherited. 若要查看扩展版本,请在 Visual Studio 命令提示中输入以下命令。To view the expanded version, enter the following command at a Visual Studio command prompt. (将占位符文件名称更改为要使用的名称。)(Change the placeholder file names to the one you want to use.)

msbuild /pp: temp .txt myapp .vcxprojmsbuild /pp: temp .txt myapp .vcxproj

除非你十分熟悉 MSBuild,否则展开的项目文件可能会很大并且难以理解。Expanded project files can be large and difficult to understand unless you are familiar with MSBuild. 这是项目文件的基本结构:Here's the basic structure of a project file:

  1. 基本项目属性,不在 IDE 中显示。Fundamental project properties, which are not exposed in the IDE.

  2. 导入 Microsoft.cpp.default.props,该文件定义了一些基本的、不依赖于工具集的属性。Import of Microsoft.cpp.default.props, which defines some basic, toolset-independent properties.

  3. 全局配置属性在“常规配置”页面上显示为“PlatformToolset”和“项目”默认属性。Global Configuration properties (exposed as PlatformToolset and Project default properties on the Configuration General page. 这些属性决定下一步将哪个工具集和内部属性表导入 Microsoft.cpp.props 中。These properties determine which toolset and intrinsic property sheets are imported in Microsoft.cpp.props in the next step.

  4. 导入 Microsoft.cpp.props,该文件设置大多数项目默认值。Import of Microsoft.cpp.props, which sets most of the project defaults.

  5. 导入所有属性表,包括 .user 文件。Import of all property sheets, including .user files. 这些属性表可以重写内容,但“PlatformToolset”和“项目”默认属性除外。These property sheets can override everything except the PlatformToolset and Project default properties.

  6. 项目配置属性的其余部分。The remainder of the project configuration properties. 这些值可以重写属性表中设置的内容。These values can override what was set in the property sheets.

  7. 项(文件)及其元数据。Items (files) together with their metadata. 这些始终是 MSBuild 评估规则的最后一项,即使它们出现在其他属性和导入之前仍是如此。These are always the last word in MSBuild evaluation rules, even if they occur before other properties and imports.

有关详细信息,请参阅 MSBuild 属性For more information, see MSBuild Properties.

添加包含目录到默认目录集Adding an include directory to the set of default directories

在将包含目录添加到项目中时,请勿重写所有默认目录,这点非常重要。When you add an include directory to a project, it is important not to override all the default directories. 添加目录的正确方法是追加新路径,例如"C:\MyNewIncludeDir\",然后追加“$(IncludePath)”宏为属性值。The correct way to add a directory is to append the new path, for example "C:\MyNewIncludeDir", and then to Append the $(IncludePath) macro to the property value.

设置生成的环境变量Setting environment variables for a build

Visual C++ 编译器 (cl.exe) 可识别某些环境变量,尤其是 LIB、LIBPATH、PATH 和 INCLUDE。The Visual C++ compiler (cl.exe) recognizes certain environment variables, specifically LIB, LIBPATH, PATH, and INCLUDE. 使用 IDE 生成时,VC++ Directories Property Page 属性页中设置的属性用于设置那些环境变量。When you build with the IDE, the properties that are set in the VC++ Directories Property Page property page are used to set those environment variables. 如果已设置了 LIB、LIBPATH 和 INCLUDE 值(例如通过开发人员命令提示设置),则这些值将被相应的 MSBuild 属性的值替换。If LIB, LIBPATH, and INCLUDE values have already been set, for example by a Developer Command Prompt, they are replaced with the values of the corresponding MSBuild properties. 然后生成在 PATH 前预置 VC++ 目录可执行目录属性的值。The build then prepends the value of the VC++ Directories executable directories property to PATH. 你可以通过创建用户定义的宏然后选中显示“在生成环境中将此宏设置为环境变量”的框来设置用户定义的环境变量。You can set a user-defined environment variable by created a user-defined macro and then checking the box that says Set this macro as an environment variable in the build environment.

设置调试会话的环境变量Setting environment variables for a debugging session

在项目“属性页”对话框的左窗格中,展开“配置属性”,然后选择“调试”。In the left pane of the project's Property Pages dialog box, expand Configuration Properties and then select Debugging.

在右窗格中,修改“环境”或“合并环境”项目设置,然后选择“确定”按钮。In the right pane, modify the Environment or Merge Environment project settings and then choose the OK button.

修改属性和目标无需更改项目文件Modifying properties and targets without changing the project file

可以从 MSBuild 命令提示符处重写项目属性和目标而无需更改项目文件。You can override project properties and targets from the MSBuild command prompt without changing the project file. 当你想要暂时或偶尔应用某些属性时,这非常有用。This is useful when you want to apply some properties temporarily or occasionally. 它假定你对 MSBuild 有一定了解。It assumes some knowledge of MSBuild. 有关详细信息,请参阅 MSBUildFor more information, see MSBUild.

重要

可以使用 Visual Studio 中的 XML 编辑器或任何文本编辑器来创建 .props 或 .targets 文件。You can use the XML Editor in Visual Studio, or any text editor, to create the .props or .targets file. 不要在此情况下使用“属性管理器”,因为它会将属性添加到项目文件中。Don't use the Property Manager in this scenario because it adds the properties to the project file.

重写项目属性:To override project properties:

  1. 创键指定要重写的属性的 .props 文件。Create a .props file that specifies the properties you want to override.

  2. 从命令提示符处设置 ForceImportBeforeCppTargets="C:\sources\my_props.props"From the command prompt: set ForceImportBeforeCppTargets="C:\sources\my_props.props"

重写项目目标:To override project targets:

  1. 创建具有其实现或特定目标的 .targets 文件Create a .targets file with their implementation or a particular target

  2. 从命令提示符处设置 ForceImportAfterCppTargets ="C:\sources\my_target.targets"From the command prompt: set ForceImportAfterCppTargets ="C:\sources\my_target.targets"

还可以使用/p: 选项在 msbuild 命令行上设置任一选项:You can also set either option on the msbuild command line by using the /p: option:

> msbuild myproject.sln /p:ForceImportBeforeCppTargets="C:\sources\my_props.props"
> msbuild myproject.sln /p:ForceImportAfterCppTargets="C:\sources\my_target.targets"

以这种方法重写属性和目标等同于将以下导入添加到该解决方案的所有 .vcxproj 文件:Overriding properties and targets in this way is equivalent to adding the following imports to all .vcxproj files in the solution:

<Import Project=="C:\sources\my_props.props" />
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<Import Project==" C:\sources\my_target.targets"" />

请参阅See also

创建和管理 Visual C++ 项目Creating and Managing Visual C++ Projects
.vcxproj 和 .props 文件结构.vcxproj and .props file structure
属性页 XML 文件Property page XML files