Поделиться через


Working with Project Properties

In the IDE, all compiler options, linker options, debugger settings, and custom build steps are exposed as properties. You use a project’s property pages to view and modify its properties. You can apply project properties independently to any combination of build configuration (Debug or Release) and target platform (Win32, x64, or ARM). You can also set properties for individual files in a project.

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 later in this article.

The Visual C++ project system is based on MSBuild. Although you can edit the XML project files and property sheets directly on the command line, we recommend that you use the IDE, especially when you modify properties that participate in inheritance. A manually-edited file that is valid in MSBuild is not necessarily readable by the Visual C++ project system and you can introduce subtle errors into your build process.

A project file is an XML file that has the file name extension .vcxproj. All the properties that you set in the IDE are written to the project file directly or to property sheets that are imported at build time.

The following illustration shows the property pages for a Visual C++ project. Notice that in the left pane, the VC++ Directories rule is selected, and in the right pane, the properties that are associated with that rule are shown. (The $(...) values are macros, which are discussed later in this article.) VC++ Directories is a configuration property, and its values can be different for different configurations—for example, Debug versus Release. You can use the Configuration and Platform list boxes at the top of the dialog box to set the configurations that the properties apply to; in many cases All Platforms and All Configurations are the right choice. Settings in the Common Properties rule apply to all configurations.

Visual C++ Property Pages

Setting properties for a project

In general, here's how to set properties for a project:

To set a property for a project

  1. On the menu bar, choose Project, Properties. Or in Solution Explorer or Property Manager, open the shortcut menu for the project and then choose Properties. The Property Pages dialog box opens.

  2. In the boxes at the top of the dialog box, select the configurations and platforms that you want the setting to apply to.

    To create or modify the configurations that appear in the Configuration box, choose the Configuration Manager button.

  3. Set the property value. When you choose the OK button, the new value is written to the project file.

Note

The Property Pages dialog box shows only the property pages that apply to the current project. For example, if the project does not have an .idl file, the MIDL property page is not displayed.

More information about properties in the Property Pages dialog box:

Tip

You can open a property page directly by typing its name in the Quick Launch window, which was introduced in Visual Studio 2012.

Hard-coded properties versus macros

Some project properties can take as a value a reference to a property that is defined elsewhere. This kind of value is referred to as a macro to distinguish it from other kinds of project property settings. A macro can refer to a property that's defined by the MSBuild system or to one that you have defined yourself. 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.

  • global macros
    Applies to all items in a project configuration. Has the syntax $(name). An example of a global macro is $(VCInstallDir), which stores the root directory of your Visual Studio installation. A global macro corresponds to a PropertyGroup in MSBuild.

  • item macros
    Has the syntax %(name). 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. 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. For example, the C/C++ Preprocessor Definitions configuration property can take a %(PreprocessorDefinitions) item macro that applies to all .cpp files in the project. This kind of item macro corresponds to an ItemDefinitionGroup metadata in MSBuild. For more information, see Item Definitions.

You can use macros to define property values, and you can use the Property Editor to view the values of available macros.

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 list box showing edit option

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

Sharing reusable property configurations

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 .prop 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. The following illustration shows Property Manager after the shortcut menu for the Release | Win32 configuration node was opened.

Property Manager shortcut menu

If you choose Add New Project Property Sheet and then select, for example, the MyProps.props property sheet, a property page dialog box appears. Notice that it applies to the MyProps property sheet; any changes you make are written to the sheet, not to the project file (.vcxproj).

New Property Sheet Option via Property Manager

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.

Important

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. 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 file. (Can override the default and property sheet settings.)

  4. Items metadata

Tip

On a property page, a property in bold is defined in the current context. A property in normal font is inherited.

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. 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**.vcxproj**

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. Fundamental project properties, which are not exposed in the IDE.

  2. Import of Microsoft.cpp.default.props, which defines some basic, toolset-independent properties.

  3. Global Configuration properties (exposed as PlatformToolset and Project default properties on the Configuration General page. These properties determine which toolset and intrinsic property sheets are imported in Microsoft.cpp.props in the next step.

  4. Import of Microsoft.cpp.props, which sets most of the project defaults.

  5. Import of all property sheets, including .user files. 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. These are always the last word in MSBuild evaluation rules, even if they occur before other properties and imports.

For more information, see MSBuild Properties.

.user files and why they are problematic

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. 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.

Although .user files are still installed by Visual Studio and participate in property inheritance, they are empty by default. 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.

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. 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.

Creating a user-defined macro

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. 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 earlier in this article.

To create a user-defined macro

  1. 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.

Setting environment variables for a build

The Visual C++ compiler (cl.exe) recognizes certain environment variables, specifically LIB, LIBPATH, PATH, and INCLUDE. 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. 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. 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.

Viewing all macros and their values

In a project's Property Pages dialog box, select a string property such as VC++ Directories, choose the down arrow button in the right column, and then choose <Edit> to open the Property Editor. In the Property Editor, choose the Macros button.

Setting properties for multiple configurations simultaneously

To set properties for all configurations, in the boxes at the top of a project's Property Pages dialog box, select All Configurations and All Platforms and then set the properties you want.

To set properties for just some configurations, multi-select them in Property Manager, and then open the shortcut menu and choose Properties. Any settings you change are applied to every configuration you selected.

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. 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.

To quickly browse and search all options

Introduced in Visual Studio 2012, the All Options property page (under the C/C++ node in the left pane of 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:
    Search only in values (case-insensitive substring).

See Also

Other Resources

Creating and Managing Visual C++ Projects