如何合并 Directory.Build.props 文件中的属性而不是覆盖属性?

Giz*_*zmo 6 msbuild msbuild-15 visual-studio-2019

我有以下Directory.Build.props文件:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <LibraryPath>$(BOOST_ROOT)\lib32-msvc-14.2</LibraryPath>
    <IncludePath>$(BOOST_ROOT)\</IncludePath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <LibraryPath>$(BOOST_ROOT)\lib64-msvc-14.2</LibraryPath>
    <IncludePath>$(BOOST_ROOT)\</IncludePath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <LibraryPath>$(BOOST_ROOT)\lib32-msvc-14.2</LibraryPath>
    <IncludePath>$(BOOST_ROOT)\</IncludePath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
    <LibraryPath>$(BOOST_ROOT)\lib64-msvc-14.2</LibraryPath>
    <IncludePath>$(BOOST_ROOT)\</IncludePath>
  </PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

每当我在子文件夹(在 Visual Studio 2019 中)中加载 (C++) 项目时,该项目都会丢失所有继承的包含路径和库路径:

继承的价值观: 继承的价值观

我想这可能是因为我没有附加到IncludePathand LibraryPath,所以我这样做并更改了props文件,如下所示:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <LibraryPath>$(LibraryPath);$(BOOST_ROOT)\lib32-msvc-14.2</LibraryPath>
    <IncludePath>$(IncludePath);$(BOOST_ROOT)\</IncludePath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <LibraryPath>$(LibraryPath);$(BOOST_ROOT)\lib64-msvc-14.2</LibraryPath>
    <IncludePath>$(IncludePath);$(BOOST_ROOT)\</IncludePath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <LibraryPath>$(LibraryPath);$(BOOST_ROOT)\lib32-msvc-14.2</LibraryPath>
    <IncludePath>$(IncludePath);$(BOOST_ROOT)\</IncludePath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
    <LibraryPath>$(LibraryPath);$(BOOST_ROOT)\lib64-msvc-14.2</LibraryPath>
    <IncludePath>$(IncludePath);$(BOOST_ROOT)\</IncludePath>
  </PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

然后重新加载项目,再次导致继承值丢失。

我仔细检查了宏是否存在,它们是(VC_WindowsSDK_宏):

宏存在

我还尝试更改Directory.Build.propsDirectory.Build.targets使其在项目加载后加载,但文件根本没有加载。

如何确保我的Directory.Build.props继承默认值并附加我的自定义选项?

我想要这样做的原因是因为我正在迁移已弃用的Microsoft.Cpp.Win32.user.props文件。

将扩展名更改为.targets仅适用于构建过程,但 intellisense / Visual Studio 不会识别包含文件,并显示很多错误(在编译之前)。这不是优选的。

Mr *_*ian 2

\n

如何确保我的 Directory.Build.props 继承默认值,并附加我的自定义选项?

\n
\n\n

我认为您不能用于Directory.Build.props覆盖项目属性 UI 中的系统属性。

\n\n

根据您的信息,LibraryPathIncludePath已经失去了这些值。请注意以下有关进口订单的信息Directory.Build.props

\n\n

Directory.Build.props 很早就导入到 Microsoft.Common.props 中,稍后定义的属性对其不可用。因此,请避免引用尚未定义的属性(并将评估为空)。

\n\n

从 NuGet 包导入 .targets 文件后,从 Microsoft.Common.targets 导入 Directory.Build.targets。因此,它可以覆盖大多数构建逻辑中定义的属性和目标,但有时您可能需要在最终导入后自定义项目文件。

\n\n

你可以参考这个链接

\n\n

详细来说,Directory.Build.props就像初始化项目并始终运行的步骤Microsoft.Common.props,在此之前为大多数系统属性(例如LibraryPath和 )设置默认值IncludePath。所以一开始,值为空是正常的。之后,当它执行时Microsoft.Common.props,会出现以下内容:

\n\n
<Includepath Condidtion="\'$(Includepath)\'==\'\'">$(VC_IncludePath)......</Includepath >\n
Run Code Online (Sandbox Code Playgroud)\n\n

而且你已经在 中$(BOOST_ROOT)\\lib32-msvc-14.2添加了,所以当你执行下一步时, 已经有了值,所以它会跳过分配系统值的操作,并且不会覆盖这一步中的值,从而导致这种奇怪的行为。IncludepathDirectory.Build.propsIncludepath

\n\n

对于Directory.Build.targets,它会在最后执行覆盖操作,当 includepath 和 LibraryPath 属性都已获取系统值时,并且覆盖没有问题。但您只能在运行项目时获取这些值,如稍后所示。

\n\n

简而言之,Directory.Build.props适合定义一些稍后会使用或覆盖的属性,Directory.Build.targets适合覆盖构建过程中已经定义的值。

\n\n

建议

\n\n

1)要设置包含目录,您可以将它们添加到 INCLUDE 环境变量中。您可以设置 INCLUDE 和 LIB 变量,如下所示:

\n\n
set INCLUDE=xxxx\nset LIB=xxxx\\lib32-msvc-14.2\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后添加 /p:"VCBuildAdditionalOptions= /useenv"到 MSBuild 参数,以便它采用 INCLUDE 和 LIB 变量。

\n\n

你可以参考这个

\n\n

2)或者您可以在路径中使用初始函数C:\\Users\\UserName\\AppData\\Local\\Microsoft\\MSBuild\\v4.0\\Microsoft.Cpp.xxx.user.props来添加额外的 includepath,librarypath,如\xef\xbc\x9a

\n\n
<?xml version="1.0" encoding="utf-8"?>\n<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">\n  <ImportGroup Label="PropertySheets">\n  </ImportGroup>\n  <PropertyGroup Label="UserMacros" />\n  <PropertyGroup>\n    <IncludePath>C:\\any-name\\include;$(IncludePath)</IncludePath>\n    <LibraryPath>C:\\any-name\\lib;$(LibraryPath)</LibraryPath>\n  </PropertyGroup>\n  <ItemDefinitionGroup />\n  <ItemGroup />\n</Project>\n
Run Code Online (Sandbox Code Playgroud)\n\n

希望它可以帮助你。

\n