Using Common Configuration Files in VS Projects with C++

When working on a solution in Visual Studio (VS) that contains multiple projects, it is common to encounter situations where multiple projects need to include the same configurations, such as C++ version, additional library directories, additional include directories, etc. This article provides methods to include common configuration files to avoid repetitive configurations.

1. Configure Third-Party Libraries and Driver Directories via Symbolic Links

For local hardware drivers and libraries that need to be included in the project, such as the <span>OpenSSL</span> driver and third-party hardware driver libraries, since the installation locations may vary for each user, or multiple versions of drivers may be installed on the computer (for example, I have multiple versions of the OpenSSL driver installed), symbolic links can be used to link the header files and library file paths of these third-party libraries and drivers to a fixed location in the project directory. This way, these third-party libraries and drivers can be included in the project using VS macros (such as solution directory <span>SolutionDir</span>, build configuration <span>Configuration</span>, etc.).

For example, the directory for <span>[email protected](x64)</span> on my computer is <span>C:\Program Files\OpenSSL-Win64-1_1_1</span>, which can be linked to the project’s <span>thirdparty</span> directory:

mklink /d "C:\Projects\hello_sln\thirdparty\OpenSSL-Win64" "C:\Program Files\OpenSSL-Win64-1_1_1"

Thus, in the project, the header file directory for <span>OpenSSL-Win64</span> can be included using the macro <span>$(SolutionDir)..\thirdparty\OpenSSL-Win64\include</span>.

Project File Organization:

hello_sln
├── app
    ├── CommonSettings.props
    └── hello_sln.sln
├── lib
    └── hello_proj
        ├── hello_sln.vcxproj.filters
        └── hello_proj.vcxproj 
└── thirdparty
    ├── OpenSSL-Win64 -> C:\Program Files\OpenSSL-Win64-1_1_1
    └── boost -> C:\local\boost_1_83_0

2. Configure Third-Party Library and Driver Directories

When creating a project under a solution in VS, we usually need to configure some properties of the project in the project properties, such as the C++ language version, additional library directories, additional include directories, etc.

Using Common Configuration Files in VS Projects with C++
Project Properties Page

If there are multiple projects under a solution, and each project needs to configure these properties, it can become cumbersome. Is it possible to create a common configuration file and include this common configuration file in each project to avoid repetitive configurations?

The answer is yes. VS provides a <span>.props</span> common configuration file, which can be included in each project. This configuration file can serve as a supplementary configuration to the project configuration file <span>.vcxproj</span>. The structures of these two files are consistent, and by including our <span>.props</span> common configuration file in the <span>.vcxproj</span>, VS can merge these two files when loading the project, thus avoiding repetitive configurations.

3. Create the Common Configuration File (props)

Next, create a <span>CommonSettings.props</span> file in the root directory of the solution with the following content:

<?xml version="1.0" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <ClCompile>
       <LanguageStandard>stdcpp17</LanguageStandard>
       <PreprocessorDefinitions>_HELLO</PreprocessorDefinitions>
      <AdditionalIncludeDirectories>$(SolutionDir)..\thirdparty;$(SolutionDir)..\thirdparty\OpenSSL-Win64\include</AdditionalIncludeDirectories>
    </ClCompile>
    <Link>
      <AdditionalLibraryDirectories>$(SolutionDir)..\thirdparty\OpenSSL-Win64\lib\VC</AdditionalLibraryDirectories>
    </Link>
  </ItemDefinitionGroup>
</Project>

This only includes the configuration for the 64-bit Debug mode; configurations for the Release mode can be added as needed.

For example, if we want all projects to use the C++17 standard, all projects need the preprocessor definition <span>_HELLO</span>, and the additional include directory to include the <span>thirdparty</span> library and driver directories, and the additional library directory to include the <span>OpenSSL-Win64/lib/VC</span> directory, we can configure the language using <span>LanguageStandard</span>, set the preprocessor definition using <span>PreprocessorDefinitions</span>, and configure the additional include directories using <span>AdditionalIncludeDirectories</span>, and the additional library directories using <span>AdditionalLibraryDirectories</span>. These are common repetitive configurations in most projects.

Then, in the project configuration file <span>hello_proj.vcxproj</span>, add the path configuration for this common configuration file:

  <Import Project="$(SolutionDir)\CommonSettings.props" Condition="exists('$(SolutionDir)\CommonSettings.props')" Label="LocalCommonSettings" />

Like this:

Using Common Configuration Files in VS Projects with C++

Then we reload the project or restart VS, and we can see that the project properties configuration has included the configurations from this common configuration file.

Using Common Configuration Files in VS Projects with C++

The project inherits configurations from our <span>CommonSettings.props</span> common configuration file. For newly added projects, simply add the configuration for <span>CommonSettings.props</span> to the beginning of the <span>.vcxproj</span> file to include our common configuration. If new common third-party drivers and libraries are introduced, configurations can be added in the <span>CommonSettings.props</span> file.

Many posts online vary in depth and even contradict each other. This article is a summary of my learning process. If you find any errors, feel free to point them out. If this article has helped you, don’t forget to like and support it; your likes are my greatest motivation for updates!~

References:

  1. .vcxproj and .props File Structure | Microsoft Learn

PS: This article is also updated on my blog GitHub – SHERlocked93/blog series. Feel free to follow my public account <span>CPP Afternoon Tea</span>, just search to add it, and I will continue to push high-quality technical articles related to CPP and its ecosystem for everyone to improve together. Let’s keep going~

Additionally, you can join the “Frontend Afternoon Tea Communication Group” by searching for <span>sherlocked_93</span> on WeChat, and please note 1 so I can add you~

Leave a Comment