【问题标题】:Building multiple platform targets at once for the same project为同一个项目一次构建多个平台目标
【发布时间】:2015-03-16 08:40:32
【问题描述】:

我有一个引用非托管 C++ DLL 的托管 C# 项目(针对任何 CPU)。我想将我的 C# 项目部署为 Any CPU,但我的 C++ DLL 没有 Any CPU 选项。我的 C++ DLL 只能针对 ARM、Win32 和 x64 平台。为方便起见,我想将所有这些构建到与我的 C# 项目相同的目录中,并让我的 C# 项目动态引用它们;我希望我的输出目录包含非托管 DLL 的 ARM、Win32 和 x64 版本。因此,如何让我的解决方案为同一个项目构建多个平台目标?

【问题讨论】:

    标签: c# visual-studio visual-c++ build platform


    【解决方案1】:

    我找到了我想要的解决方法。

    您需要做的第一件事是在您的项目文件上使用 MSBuild,以便在另一个平台下构建 DLL。我的解决方案将我的项目构建为 x64,因此我需要构建 x86 版本作为额外的构建后步骤,所以我所做的就是在我的托管 C# 项目中添加了一个构建后脚本以促进此过程:

    del "$(TargetDir)Unmanaged_x64.dll"
    ren "$(TargetDir)Unmanaged.dll" Unmanaged_x64.dll
    del "$(TargetDir)Unmanaged_x86.dll"
    "C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe" "$(SolutionDir)Unmanaged\Unmanaged.vcxproj" /p:Configuration=$(ConfigurationName) /p:Platform=x86
    copy "$(SolutionDir)Unmanaged\$(ConfigurationName)\Unmanaged.dll" "$(TargetDir)Unmanaged_x86.dll"
    

    现在,在我的 C# 应用程序中,我的 App.xaml.cs 文件中有一个程序集解析器:

    protected override void OnStartup(StartupEventArgs e)
    {
        ...
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        ...
    }
    
    Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        if (args.Name == "Unmanaged, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")
        {
            if (Environment.Is64BitOperatingSystem)
            {
                return Assembly.LoadFrom(String.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, "Unmanaged_x64.dll"));
            }
            else
            {
                return Assembly.LoadFrom(String.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, "Unmanaged_x86.dll"));
            }
        }
        return null;
    }
    

    非常适合我的需求;可能需要为你的调整!大家干杯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多