我有一个混合模式 C++ 项目,它生成一个导出一些 CLR 类的托管 dll 程序集(称为 Managed.dll)。该项目使用的是 native dll,(称为 Native.dll)。
当我从另一个生成 Client.exe 的项目中引用 Managed.dll 时,一切正常,除了我需要手动将 Native.dll 复制到与 Client.exe 相同的文件夹中。
如果有办法说服 VS 在本地(在 Client.exe 的 bin 文件夹中)复制 Managed.dll 和 Native.dll?
我尝试将 Native.dll 作为依赖程序集包含在 list 中,但这没有帮助。
编辑
Managed.dll 将成为一个可再发行程序集。它将安装在“C:\Program Files.....”的文件夹中。当使用 Visual Studio 的开发人员添加对 Managed.dll 的引用时,Native.dll 也应复制到其项目的\bin 文件夹中。
最佳答案
有几种方法可以告诉 VS 将 dll 复制到目标文件夹:
1.将dll添加为项目的资源。如果 dll 较新,则告诉 VS 复制它
2.添加一个引用dll项目的新项目,并将OutDir设置为您想要的文件夹。这个项目除了复制 dll 什么都不做。
3.在vcxproj文件中使用PostBuildEvent
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
</ClCompile>
<Link>
</Link>
<PostBuildEvent>
<Command>
echo off
mkdir "$(ProjectDir)..\..\bin\$(Configuration)\"
copy "$(OutDir)xxx.dll" "$(ProjectDir)..\..\lib\$(Configuration)\"
echo on
</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
4.在vcxproj文件中使用PreBuildEvent
5.在vcxproj文件中使用CustomBuild
<ItemGroup>
<CustomBuild Include="..\..\xxx.dll">
<FileType>Document</FileType>
<Command>
call mkdir "$(OutDir)" 2>nul &
copy /Y "..\..\xxx.dll" "$(OutDir)xxx.dll"
</Command>
<Message>Copying xxx.dll to $(OutDir)\xxx.dll</Message>
<Outputs>$(OutDir)\xxx.dll</Outputs>
</CustomBuild>
</ItemGroup>
6.使用makefile并在makefile中复制dll。并使用nmake构建
7.编写一个bat文件进行复制,调用bat文件如3-6
8.使用python等脚本,也可以从网上下载dll。并按照3-6调用py文件。
9.其他构建工具也可以提供帮助,例如 gradle
10.把它变成一个 NuGet 插件
11.有时我只是写一个bat,然后手动执行bat。
更新01(自解压dll示例):
1.将您的 native dll 添加为托管 dll 的资源
2.添加这个init()方法
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace DllSelfExtract
{
public class SelfExtract
{
public static void Init()
{
String managedDllPath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
String nativeDllPath = managedDllPath.Replace("file:///", "").Replace("DllSelfExtract.DLL", "TestDll.dll");
if(!File.Exists(nativeDllPath))
{
Stream dllIn = Assembly.GetExecutingAssembly().GetManifestResourceStream("DllSelfExtract.TestDll.dll");
if (dllIn == null) return;
using (Stream outFile = File.Create(nativeDllPath))
{
const int sz = 4096;
byte[] buf = new byte[sz];
while (true)
{
int nRead = dllIn.Read(buf, 0, sz);
if (nRead < 1)
break;
outFile.Write(buf, 0, nRead);
}
}
}
//LoadLibrary Here
}
}
}
3.在使用你的托管 dll 的项目中,首先调用 init() 方法
SelfExtract.Init();
更新 02(NuGet 示例):
1.新建一个NuGet项目
2.将托管程序集放在/lib目录中
3.将非托管的共享库及相关文件放在/build子目录下,并将所有非托管的*.dll重命名为*.dl_
4.在/build 子目录中添加一个自定义的 .targets 文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<AvailableItemName Include="NativeBinary" />
</ItemGroup>
<ItemGroup>
<NativeBinary Include="$(MSBuildThisFileDirectory)*">
<TargetPath></TargetPath>
</NativeBinary>
</ItemGroup>
<PropertyGroup>
<PrepareForRunDependsOn>
$(PrepareForRunDependsOn);
CopyNativeBinaries
</PrepareForRunDependsOn>
</PropertyGroup>
<Target Name="CopyNativeBinaries" DependsOnTargets="CopyFilesToOutputDirectory">
<Copy SourceFiles="@(NativeBinary)"
DestinationFiles="@(NativeBinary->'$(OutDir)\%(TargetPath)\%(Filename).dll')"
Condition="'%(Extension)'=='.dl_'">
<Output TaskParameter="DestinationFiles" ItemName="FileWrites" />
</Copy>
<Copy SourceFiles="@(NativeBinary)"
DestinationFiles="@(NativeBinary->'$(OutDir)\%(TargetPath)\%(Filename).%(Extension)')"
Condition="'%(Extension)'!='.dl_'">
<Output TaskParameter="DestinationFiles" ItemName="FileWrites" />
</Copy>
</Target>
</Project>
5.在Package.nuspec中为构建文件夹添加构建规则
<files>
<file src="lib\" target="lib" />
<file src="tools\" target="tools" />
<file src="content\" target="content" />
<file src="build\" target="build" />
</files>
6.构建包
7.在您的其他 C# 项目中,只需添加此 NuGet 包。
关于c++ - 在 Visual Studio 项目中本地复制 native 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39295394/