为所有项目设置 OutDir 和 IntDir

时间:2021-02-05 18:37:50

标签: c++ visual-studio project-properties

我正在尝试为解决方案中的所有项目设置 OutDirIntDir 的默认位置。如:<OutDir>$(SolutionDir)bld\$(Platform)\$(ProjectName)\$(Configuration)\</OutDir>

当我在 Directory.Build.props 文件中设置它时,它在 Visual Studio 属性对话框中看起来没问题;但是当我构建时,$(ProjectName) 部分是空的。这告诉我在 Directory.Build.props 中读取 OutDir 时此宏不可用。

我尝试将其添加到 Directory.Build.targets 文件中,但它似乎被完全忽略了。

另一个目标是在此初始更改后不修改任何 vcxproj 文件。新项目会自动继承这些设置。

这可能吗?也许我将设置放在文件中错误的位置/顺序......?

这是 Directory.Build.props 的一个片段:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Label="PropertySheets">
  </ImportGroup>

  <!-- Define macros for easy and consistent access to various parts of the tree -->
  <PropertyGroup Label="UserMacros">
    <GslInclude>$(SolutionDir)packages\Microsoft.Gsl.0.1.2.1\build\native\include</GslInclude>
    <mySrcDir>$(SolutionDir)src\</mySrcDir>
    <myBldDir>$(SolutionDir)bld\</myBldDir>
    <myBldIntDir>$(SolutionDir)bld_int\</myBldIntDir>
  </PropertyGroup>
  <ItemDefinitionGroup />
  <ItemGroup>
    <BuildMacro Include="GslInclude"> <Value>$(GslInclude)</Value> </BuildMacro>
    <BuildMacro Include="mySrcDir"> <Value>$(mySrcDir)</Value> </BuildMacro>
    <BuildMacro Include="myBldDir"> <Value>$(myBldDir)</Value> </BuildMacro>
    <BuildMacro Include="myBldIntDir"> <Value>$(myBldIntDir)</Value> </BuildMacro>
  </ItemGroup>

  <!-- Platform Toolset, Windows SDK -->
  <PropertyGroup Label="Globals">
    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
  </PropertyGroup>

  <!-- Default Compiler settings -->
  <ItemDefinitionGroup>
    <ClCompile>
      <!-- .... -->
    </ClCompile>
  </ItemDefinitionGroup>

  <!-- Default Folders for target output -->
  <PropertyGroup>
    <OutDir>$(myBldDir)$(Platform)\$(ProjectName)\$(Configuration)\</OutDir>
    <IntDir>$(myBldIntDir)$(Platform)\$(ProjectName)\$(Configuration)\</IntDir>
  </PropertyGroup>

</Project>

当然,我只是删除了 proj 文件中的所有 <OutDir><IntDir> 设置。

我还希望能够根据 Visual Studio 的版本对设置设置条件。我们的一些开发人员正在将 VS2017 与较旧的 ToolSet 一起使用;一些正在使用最新的 VS2019....

2 个答案:

答案 0 :(得分:0)

这是可能的,而且它实际上是在项目之间共享设置的推荐方式。唯一需要注意的是,该方案对部分标签和内含物的顺序/位置相当敏感。以下应该适用于 OutDirIntDir

Directory.Build.props

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <_PropertySheetDisplayName>Directory.Build</_PropertySheetDisplayName>
  </PropertyGroup>
  <ImportGroup Label="PropertySheets" />
  <PropertyGroup Label="UserMacros">
    <!-- other user macros -->
    <OutDir> ... </OutDir>
    <IntDir> ... </IntDir>
  </PropertyGroup>
  <!-- rest of .props file --->

Project.vcxproj

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- ... -->
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <!-- include custom .props here, can also use full or relative path -->
  <ImportGroup Label="PropertySheets">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
    <Import Project="Directory.Build.props" />
  </ImportGroup>
  <!-- ... -->

答案 1 :(得分:0)

感谢为此提供帮助的人。我让它工作了 - 至少我的主要目标是为构建“输出”建立一个预定义的位置。

从这里参考信息:https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2019 并在 VS 道具文件中搜索,我找到了一个宏“MSBuildProjectName”,它在过程的早期设置。

我在构建树的根目录中创建了一个 Directory.Build.props 文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Label="PropertySheets">
  </ImportGroup>

  <!-- Define macros for easy and consistent access to various parts of the tree -->
  <PropertyGroup Label="UserMacros">
    <GslInclude>$(SolutionDir)packages\Microsoft.Gsl.0.1.2.1\build\native\include</GslInclude>
    <myBldDir>$(SolutionDir)bld\</myBldDir>
    <myBldIntDir>$(SolutionDir)bld_int\</myBldIntDir>
  </PropertyGroup>
  <ItemDefinitionGroup />
  <ItemGroup>
    <BuildMacro Include="GslInclude"> <Value>$(GslInclude)</Value> </BuildMacro>
    <BuildMacro Include="myBldDir"> <Value>$(myBldDir)</Value> </BuildMacro>
    <BuildMacro Include="myBldIntDir"> <Value>$(myBldIntDir)</Value> </BuildMacro>
</BuildMacro>
    <!-- etc .... -->
  </ItemGroup>

  <!-- Platform Toolset, Windows SDK -->
  <PropertyGroup Label="Globals">
    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
  </PropertyGroup>
  <PropertyGroup Label="Configuration">
    <PlatformToolset>v142</PlatformToolset>
  </PropertyGroup>

  <PropertyGroup>
    <IntDir>$(myBldIntDir)$(Platform)\$(MSBuildProjectName)\$(Configuration)\</IntDir>
    <OutDir>$(myBldDir)$(Platform)\$(MSBuildProjectName)\$(Configuration)\</OutDir>
  </PropertyGroup>

  <!-- lots more ....  -->
</Project>

此文件中的设置会影响文件夹结构中位于它“下方”的所有项目。 如果子文件夹需要不同的值,请在导入“父”道具文件的文件夹中添加 Directory.Build.props(可能在上面几层):

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />

  <ImportGroup Label="PropertySheets">
  </ImportGroup>

  <PropertyGroup>
    <IntDir>$(myBldIntDir)$(Platform)\Tests\$(MSBuildProjectName)\$(Configuration)\</IntDir>
    <OutDir>$(myBldDir)$(Platform)\Tests\$(MSBuildProjectName)\$(Configuration)\</OutDir>
  </PropertyGroup>

</Project>

“子”道具文件中的设置会覆盖其父道具中设置的任何内容。

一旦我删除了 XML 项目文件中的所有 <OutDir><IntDir> 元素,“输出目录”和“中间目录”设置就会以常规字体显示(不是粗体,表示它没有继承),当然,当我构建时,一切都会进入合并的文件夹树;远离源头。很酷的事情:新项目将自动填充这些新的默认设置。

注意:@dxiv 指出某些设置可能更挑剔,需要更多工作。