問題描述:
需要使用的C++類庫區分x64和x86版本,但C#項目只能使用一種需要編碼X64、X86版本,有不能在編譯時經常改引用DLL,能不能再編譯時根據編譯選項自動選擇dll庫版本並復制到輸出路徑
解決:
1.准備dll庫編譯好的兩個版本;
2.在C#項目中引用隨編一個(一般x64,x86引用文件都一致);
3.卸載該項目后,修改C#工程文件(*.csproj):
3.1 找到自動生成的引用節點
<Reference Include="CefSharp">
<HintPath>DLLs\x64\CefSharp.dll</HintPath>
</Reference>
3.2 添加引用條件 Condition=" '$(Platform)' == 'x64'"
<Reference Include="CefSharp" Condition=" '$(Platform)' == 'x64'">
<HintPath>DLLs\x64\CefSharp.dll</HintPath>
</Reference>
3.3 同理X86版本進行修改
<Reference Include="CefSharp" Condition=" '$(Platform)' == 'x86'">
<HintPath>DLLs\x86\CefSharp.dll</HintPath>
</Reference>
4.保存后重新加載該工程即可;
Note:
1.貌似也可以動態指定路徑:
<Reference Include="CefSharp.Core">
<HintPath>DLLs\$(Platform)\CefSharp.Core.dll</HintPath>
</Reference>
2.添加自動復制腳本
2.1 添加targets文件,在該文件中定義所含目標文件(文件內容):
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup Condition="Exists('$(MSBuildThisFileDirectory)\DLLs\x86')"> <CefDlls32 Include="$(MSBuildThisFileDirectory)\DLLs\x86\*.*" /> </ItemGroup> <ItemGroup Condition="Exists('$(MSBuildThisFileDirectory)\DLLs\x64')"> <CefDlls64 Include="$(MSBuildThisFileDirectory)\DLLs\x64\*.*" /> </ItemGroup> </Project>
2.2 添加props文件,並在文件中寫入復制信息以及路徑:
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Target Name="CefSharpCopyDlls86" BeforeTargets="AfterBuild" Condition="'$(Platform)' == 'x86'"> <Message Importance="high" Text="Copying cef.dlls x86 binaries" /> <Copy SourceFiles="@(CefDlls32)" DestinationFolder="$(TargetDir)" SkipUnchangedFiles="true" /> </Target> <Target Name="CefSharpCopyDlls64" BeforeTargets="AfterBuild" Condition="'$(Platform)' == 'x64'"> <Message Importance="high" Text="Copying cef.dlls x64 binaries" /> <Copy SourceFiles="@(CefDlls64)" DestinationFolder="$(TargetDir)" SkipUnchangedFiles="true" /> </Target> </Project>
2.3 在項目文件中導入兩個文件,ok;
<Import Project="DLLs\CefDlls.Native.props" Condition="Exists('DLLs\CefDlls.Native.props')" />
<Import Project="Dlls\cef.dlls.targets" Condition="Exists('Dlls\cef.dlls.targets')" />
2.4 驗證成功,編譯輸出信息;
