Visual Studio 2019 Tips-複数プロジェクトの共通設定取り扱い

初めに

Visual Studio 2019 Tips-Linuxクロス開発1

上記では、共有プロジェクトを参照しているとリモートマシン側の実行モジュール配置位置が異なり、そのため、プロジェクトに対して特別な記述が必要だと書いた。

もし、同様なプロジェクトが複数あった場合、すべてのプロジェクトにその記述をするのは手間になり、また記述ミスや、記載内容の修正があった場合の修正時に、修正漏れなど発生するかもしれない。

そこでソリューションの中の複数プロジェクトで同じ設定を行う場合には、Importが使える。

プロジェクトファイルの記述

C++のプロジェクトファイル(拡張子がvcxproj)は、以下のようなXMLの記述で実現されている。

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|ARM">
      <Configuration>Debug</Configuration>
      <Platform>ARM</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|ARM">
      <Configuration>Release</Configuration>
      <Platform>ARM</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Debug|ARM64">
      <Configuration>Debug</Configuration>
      <Platform>ARM64</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|ARM64">
      <Configuration>Release</Configuration>
      <Platform>ARM64</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Debug|x86">
      <Configuration>Debug</Configuration>
      <Platform>x86</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|x86">
      <Configuration>Release</Configuration>
      <Platform>x86</Platform>

プロジェクトのプロパティに表示されている内容が、この中に入っている。

ただすべてがこのファイルに記載されているわけではなく、多くは別ファイルに書かれているものを利用しているような形になっている。

この別ファイルの中身を利用するというのがImportの記述になる。

Importについて

実際の記述は以下の様になる。

  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />

実際の動作としては、C言語のincludeと同じような動作になる。
上記は、$(VCTargetsPath)\Microsoft.Cpp.propsを読み込み、それをプロジェクトのプロパティとして利用するという意味になる。

共通して使用するプロパティの対応

複数のプロジェクトに共通するプロパティについては、その設定内容を別ファイルに保存しておき、プロジェクトでImportするということができる。

前回のVisual Studio 2019 Tips-Linuxクロス開発1ではビルドイベント-リモートのビルド後イベントのコマンドラインに手を加える必要があったのだが、それを以下の様に別ファイル化。

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">
    <RemotePostBuildEvent>
      <Command>test -d $(RemoteRootDir)/$(ProjectName)/bin || ln -s -r $(RemoteRootDir)/$(ProjectName)/$(ProjectName)/bin $(RemoteRootDir)/$(ProjectName)/</Command>
    </RemotePostBuildEvent>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">
    <RemotePostBuildEvent>
      <Command>test -d $(RemoteRootDir)/$(ProjectName)/bin || ln -s -r $(RemoteRootDir)/$(ProjectName)/$(ProjectName)/bin $(RemoteRootDir)/$(ProjectName)/</Command>
    </RemotePostBuildEvent>
  </ItemDefinitionGroup>
</Project>

上記ファイルを、C++プロジェクト内で以下のようImport定義することで、参照することができるようになる。(ファイルはソリューション直下にcommon-linux.propsとして配置)

<Import Project="$(SolutionDir)\common-linux.props" />

複数のプロジェクトが含まれる大規模ソリューションでは有効なものになると思われる。

コメント

タイトルとURLをコピーしました