中构建时有条件地使用

中构建时有条件地使用

本文介绍了在 Visual Studio 中构建时有条件地使用 32/64 位引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以 32/64 位构建并具有相应 32/64 位依赖项的项目.我希望能够切换配置并使用正确的引用,但我不知道如何告诉 Visual Studio 使用适合体系结构的依赖项.

I have a project that builds in 32/64-bit and has corresponding 32/64-bit dependencies. I want to be able to switch configurations and have the correct reference used, but I don't know how to tell Visual Studio to use the architecture-appropriate dependency.

也许我的方法不对,但我希望能够在配置下拉列表中在 x86 和 x64 之间切换,并使引用的 DLL 正确.

Maybe I'm going about this the wrong way, but I want to be able to switch between x86 and x64 in the configuration dropdown, and have the referenced DLL be the right bitness.

推荐答案

这是我在以前的项目中所做的,这将需要 .csproj 文件的手动版本.您还需要用于不同二进制文件的单独目录,最好是彼此的同级文件,并且与您的目标平台同名.

Here is what I've done in a previous project, which will require the manual edition of the .csproj file(s). You also need separate directories for the different binaries, ideally siblings of each other, and with the same name as the platform you are targeting.

将单个平台的引用添加到项目后,在文本编辑器中打开 .csproj.在 元素中的第一个 元素之前,添加以下代码,这将有助于确定您在哪个平台上运行(和构建).

After adding a single platform's references to the project, open the .csproj in a text editor. Before the first <ItemGroup> element within the <Project> element, add the following code, which will help determine which platform you're running (and building) on.

<!-- Properties group for Determining 64bit Architecture -->
<PropertyGroup>
  <CurrentPlatform>x86</CurrentPlatform>
  <CurrentPlatform Condition="'$(PROCESSOR_ARCHITECTURE)'=='AMD64' or '$(PROCESSOR_ARCHITEW6432)'=='AMD64'">AMD64</CurrentPlatform>
</PropertyGroup>

然后,对于特定于平台的引用,您进行如下更改:

Then, for your platform specific references, you make changes such as the following:

<ItemGroup>
  <Reference Include="Leadtools, Version=16.5.0.0, Culture=neutral, PublicKeyToken=9cf889f53ea9b907, processorArchitecture=x86">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>....LibLeadtools$(CurrentPlatform)Leadtools.dll</HintPath>
  </Reference>
  <Reference Include="Leadtools.Codecs, Version=16.5.0.0, Culture=neutral, PublicKeyToken=9cf889f53ea9b907, processorArchitecture=x86">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>....LibLeadtools$(CurrentPlatform)Leadtools.Codecs.dll</HintPath>
  </Reference>
  <Reference Include="Leadtools.ImageProcessing.Core, Version=16.5.0.0, Culture=neutral, PublicKeyToken=9cf889f53ea9b907, processorArchitecture=x86">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>....LibLeadtools$(CurrentPlatform)Leadtools.ImageProcessing.Core.dll</HintPath>
  </Reference>
  <Reference Include="System" />
  <Reference Include="System.Core" />
  <Reference Include="System.Data.Entity" />
  <!--  Other project references -->
</ItemGroup>

注意我们在上面定义的 $(CurrentPlatform) 属性的使用.相反,您可以针对哪个程序集包含哪个平台使用条件.您可能还需要:

Note the use of the $(CurrentPlatform) property, which we defined above. You could, instead, use conditionals for which assemblies to include for which platform. You could also need to:

  • $(PROCESSOR_ARCHITEW6432)$(PROCESSOR_ARCHITECTURE) 替换为 $(Platform) 以仅考虑项目的目标平台
  • 更改平台确定逻辑以适合当前机器,这样您就不会构建/引用 64 位二进制文​​件以在 32 位平台上执行.
  • Replace the $(PROCESSOR_ARCHITEW6432) and $(PROCESSOR_ARCHITECTURE) with $(Platform) to consider ONLY the target platform of the projects
  • Alter the platform determination logic in order to be appropriate to the current machine, so that you're not building/referencing a 64 bit binary to execute on a 32 bit platform.

我最初是为工作中的内部 Wiki 编写的,但是,我对其进行了修改并发布了 完整过程到我的博客,如果您对详细的分步说明感兴趣.

I had this written up originally for an internal Wiki at work, however, I've modified it and posted the full process to my blog, if you are interested in the detailed step-by-step instructions.

这篇关于在 Visual Studio 中构建时有条件地使用 32/64 位引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:39