C# 如何添加外部原生依赖dll?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16420595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add external native dependency dll?
提问by Denis
I have two projects. First is a Windows Forms Application project and second is a class library project. Сlass library project works with FANN. Windows Forms is Startup Project.
我有两个项目。第一个是 Windows 窗体应用程序项目,第二个是类库项目。Сclass 库项目与FANN一起使用。Windows 窗体是启动项目。
I should have Fann.Net.dll and fanndoubleMT.dllto work with the FANN. I downloaded these libraries and put their in a folder lib, located in the root of the solution.
我应该让Fann.Net.dll 和 fanndoubleMT.dll 与 FANN一起工作。我下载了这些库并将它们放在位于解决方案根目录的文件夹lib中。
I added Fann.Net.dll as external dll to the class library project. I compiled the project. I got an error that says "Unable to load DLL 'fanndoubleMT.dll'. I fixed this error by adding fanndoubleMT.dll to the folder Windows_Forms_Application\bin\Debug.
我将 Fann.Net.dll 作为外部 dll 添加到类库项目中。我编译了这个项目。我收到一条错误消息,显示“无法加载 DLL 'fanndoubleMT.dll'。我通过将 fanndoubleMT.dll 添加到文件夹 Windows_Forms_Application\bin\Debug 来修复此错误。
I think this is a terrible solution to the problem, because I use git, and every time you need to transfer dll to this folder on the new workplace.
我认为这是一个很糟糕的问题解决方案,因为我使用git,并且每次都需要将dll传输到新工作场所的这个文件夹中。
Sincerely, Denis.
真诚的,丹尼斯。
采纳答案by liang
You may try this:
你可以试试这个:
- Add/Existing Item, instead of Add Reference.
- Use Add As Link.
- Make sure the item is to be copied in build folder. In the property of the library in VS, set Build Action to Content and Copy to Output Directory to Copy if Newer.
- Done. Rebuild and test.
- 添加/现有项目,而不是添加参考。
- 使用添加为链接。
- 确保项目要复制到构建文件夹中。在VS中库的属性中,将Build Action设置为Content,将Copy to Output Directory设置为Copy if Newer。
- 完毕。重建和测试。
Suggested in the link http://social.msdn.microsoft.com/Forums/en-US/1b1b316a-8648-4243-a651-84de51fd2508/reference-native-dll-from-managed-c-project?forum=vssmartdevicesvbcs.
回答by Steven Licht
You cannot 'Add Reference' to unmanaged dlls. One solution is to add a Post Build Event your Windows Forms project. Something like: xcopy ..\lib\fanndoubleMT.dll $(TargetPath) The post build event can also execute a .cmd or .bat file
您不能“添加引用”到非托管 dll。一种解决方案是在您的 Windows 窗体项目中添加构建后事件。类似于: xcopy ..\lib\fanndoubleMT.dll $(TargetPath) post build 事件也可以执行 .cmd 或 .bat 文件
You still need the Reference to the managed assembly, 'Fann.Net.dll'
您仍然需要对托管程序集的引用,“Fann.Net.dll”
回答by Yochai Timmer
You can add the native dll as a linked item, and use "Copy if newer".
The problem with native dlls, is that sometimes you'll want to use different dlls according to the project's configuration (Debug/Release or platform).
您可以将本机 dll 添加为链接项,并使用“如果更新则复制”。
本机 dll 的问题在于,有时您需要根据项目的配置(调试/发布或平台)使用不同的 dll。
You can edit the project's .csproj and link the native dll conditionally:
您可以编辑项目的 .csproj 并有条件地链接本机 dll:
<ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|Win32' ">
<Content Include="..\..\..\..\..\bin\Win32\Release\fanndoubleMT.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|Win32' ">
<Content Include="..\..\..\..\..\bin\Win32\Debug\fanndoubleMT_d.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
<Content Include="..\..\..\..\..\bin\x64\Debug\fanndoubleMT_d.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
<Content Include="..\..\..\..\..\bin\x64\Release\fanndoubleMT.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
Note the copy option is set to PreserveNewestwhich means "copy if newer".
请注意,复制选项设置为PreserveNewest,这意味着“如果更新则复制”。
回答by Luzing
- Right click on your project
- Choose Add, then Reference...
- In the Reference Manager window click on Browse... (located at the bottom).
- Locate your .dll and then press Add.
- 右键单击您的项目
- 选择添加,然后参考...
- 在 Reference Manager 窗口中单击 Browse...(位于底部)。
- 找到您的 .dll,然后按添加。
回答by Cüneyt Bozyel
If the dll is not in the project bin file, you should allow the dll to be copied.
如果 dll 不在项目 bin 文件中,则应允许复制 dll。
Right click on your dll
Click properties
If the Copy to Output Directory is Do not copy, select Copy always
Rebuild the project. It will appear.
右键单击您的 dll
单击属性
如果“复制到输出目录”为“不复制”,请选择“始终复制”
重建项目。它会出现。
回答by Tomas Kubes
The above solution written by liang works only for flat project structure!You may want to organize all your DLLs in your solution into one folder named "Dependecies". But beware that files are copied relatively to project structure in the Solution Explorer. (tested with Visual Studio 2015)
以上liang写的解决方案只适用于扁平化的项目结构!您可能希望将解决方案中的所有 DLL 组织到一个名为“Dependencies”的文件夹中。但请注意,文件是相对于解决方案资源管理器中的项目结构复制的。(使用 Visual Studio 2015 测试)
- Create Folder Dependencies in your Solution Explorer
- Add/Existing Item, instead of Add Reference.
- Use Add As Link.
- In the property of the library in VS, set Build Action to Content and Copy to Output Directory to Copy if Newer.
- 在解决方案资源管理器中创建文件夹依赖项
- 添加/现有项目,而不是添加参考。
- 使用添加为链接。
- 在VS中库的属性中,将Build Action设置为Content,将Copy to Output Directory设置为Copy if Newer。
Now you should have following Solution Explorer structure:
现在您应该具有以下解决方案资源管理器结构:
Your Project
- class1.cs
- Dependencies\Fann.Net.dll
- Dependencies\fanndoubleMT.dll
Add postbuild step:
添加后期构建步骤:
xcopy "$(TargetDir)\Dependencies" "$(TargetDir)" /s /e /h /Y
This solution combining adding files to project and creating post build step has following advantages:
这种将文件添加到项目和创建后期构建步骤相结合的解决方案具有以下优点:
- Project is well organized
- No need to modify post build step if someone add a new dependency to solution explorer later,
- If you use subversion, it will reset readonly flag of locked file in repository
- 项目组织良好
- 如果稍后有人向解决方案资源管理器添加新依赖项,则无需修改构建后步骤,
- 如果您使用 subversion,它将重置存储库中锁定文件的只读标志

