有条件使用32/64位参考在Visual Studio中建立时,有条件、中建、Visual、Studio

2023-09-02 01:20:55 作者:如果猜不透那就一个人过

我有一个建立在六十四分之三十二位,并有相应的六十四分之三十二位相关性的项目。我希望能够切换配置,并且具有正确的参考用,但我不知道如何告诉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.

推荐答案

下面是我在previous项目,该项目将需要的.csproj文件(S)的手动版本所做的一切。您还需要单独的目录为不同的二进制文件,对方理想的兄弟姐妹,并与同名的平台,你的目标。

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。在第一个< ItemGroup> &LT内的元素;项目> 元素中,添加以下code,这将有助于确定哪个平台正在运行(与建筑)上。

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 either either:

替换 $(PROCESSOR_ARCHITEW6432) $(PROCESSOR_ARCHITECTURE) $(平台)要考虑项目的唯一目标平台 修改平台确定逻辑,以便适合当前的机器,让你不建/引用一个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,但是,我已经修改了它,并发布了full流程到我的博客,如果​​你有兴趣在详细的一步一步的指示。

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.