在Visual Studio中构建时,有条件地使用32/64位引用

Jon*_*Yee 122 .net 64-bit 32bit-64bit visual-studio

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

也许我正在以错误的方式解决这个问题,但我希望能够在配置下拉列表中切换x86和x64,并使引用的DLL成为正确的位数.

Hug*_*ugo 97

这是我在之前的项目中所做的,这将需要手动编辑.csproj文件.您还需要为不同的二进制文件创建单独的目录,理想情况是彼此的兄弟姐妹,并且与您要定位的平台具有相同的名称.

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

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

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

<ItemGroup>
  <Reference Include="Leadtools, Version=16.5.0.0, Culture=neutral, PublicKeyToken=9cf889f53ea9b907, processorArchitecture=x86">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>..\..\Lib\Leadtools\$(CurrentPlatform)\Leadtools.dll</HintPath>
  </Reference>
  <Reference Include="Leadtools.Codecs, Version=16.5.0.0, Culture=neutral, PublicKeyToken=9cf889f53ea9b907, processorArchitecture=x86">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>..\..\Lib\Leadtools\$(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>..\..\Lib\Leadtools\$(CurrentPlatform)\Leadtools.ImageProcessing.Core.dll</HintPath>
  </Reference>
  <Reference Include="System" />
  <Reference Include="System.Core" />
  <Reference Include="System.Data.Entity" />
  <!--  Other project references -->
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

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

  • 更换$(PROCESSOR_ARCHITEW6432)$(PROCESSOR_ARCHITECTURE)$(Platform)只考虑项目的目标平台
  • 更改平台确定逻辑以适合当前计算机,以便您不构建/引用64位二进制文​​件以在32位平台上执行.

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

  • 有趣,我的搜索带我到这里,我只需要这个,因为我也在使用LeadTools ...... +1 (7认同)

Jus*_*zer 58

AFAIK,如果你的项目需要32位或64位特定的引用(即COM-interop程序集),并且你没有兴趣手动编辑.csproj文件,那么你将不得不创建单独的32位和64位项目.

我应该注意以下解决方案未经测试,但应该可行.如果您愿意手动编辑.csproj文件,那么您应该能够通过单个项目实现所需的结果..csproj文件只是一个MSBuild脚本,因此有关完整参考,请查看此处.在编辑器中打开.csproj文件后,找到<Reference>元素.您应该能够将这些元素拆分为3个不同的项组:非特定于平台的引用,特定于x86的引用和特定于x64的引用.

下面是一个示例,假设您的项目配置了名为"x86"和"x64"的目标平台

<!-- this group contains references that are not platform specific -->
<ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <!-- any other references that aren't platform specific -->
</ItemGroup>

<!-- x86 specific references -->
<ItemGroup Condition=" '$(Platform)' == 'x86' ">
    <Reference Include="MyComAssembly.Interop">
        <HintPath>..\..\lib\x86\MyComAssembly.Interop.dll</HintPath>
    </Reference>

    <!-- any additional x86 specific references -->
</ItemGroup>

<!-- x64 specific referneces -->
<ItemGroup Condition=" '$(Platform)' == 'x64' ">
    <Reference Include="MyComAssembly.Interop">
        <HintPath>..\..\lib\x64\MyComAssembly.Interop.dll</HintPath>
    </Reference>

    <!-- any additional x64 specific references -->
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

现在,当您将项目/解决方案构建配置设置为以x86或x64平台为目标时,它应该在每种情况下都包含适当的引用.当然,你需要玩弄<Reference>元素.您甚至可以在添加x86和x64引用的位置设置虚拟项目,然后只需将<Reference>这些虚拟项目文件中的必要元素复制到"真实"项目文件中.


编辑1
这是一个常见的MSBuild项目项目的链接,我不小心从原始帖子中遗漏了这些项目:http://msdn.microsoft.com/en-us/library/bb629388.aspx


Yoc*_*mer 20

您可以将条件用于ItemGroup以获取项目文件中的dll引用.
这将导致visual studio在您更改活动配置时重新检查条件和引用.
只需为每个配置添加一个条件.

例:

 <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
    <Reference Include="DLLName">
      <HintPath>..\DLLName.dll</HintPath>
    </Reference>
    <ProjectReference Include="..\MyOtherProject.vcxproj">
      <Project>{AAAAAA-000000-BBBB-CCCC-TTTTTTTTTT}</Project>
      <Name>MyOtherProject</Name>
    </ProjectReference>
  </ItemGroup>
Run Code Online (Sandbox Code Playgroud)


Mic*_*cke 7

我在我的项目中引用了位于例如\ component\v3_NET4中的x86 DLL.x86/x64的特定DLL位于名为"x86"和"x64"的子文件夹中.

然后我使用预构建脚本,根据$(PlatformName)将适当的DLL(x86/x64)复制到引用的文件夹中.

xcopy /s /e /y "$(SolutionDir)..\component\v3_NET4\$(PlatformName)\*" "$(SolutionDir)..\component\v3_NET4"
Run Code Online (Sandbox Code Playgroud)

适合我.