How to add external native dependency dll?

久未见 提交于 2019-11-28 02:30:16

问题


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.

I should have Fann.Net.dll and fanndoubleMT.dll to work with the FANN. I downloaded these libraries and put their in a folder lib, located in the root of the solution.

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.

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.

Sincerely, Denis.


回答1:


You may try this:

  1. Add/Existing Item, instead of Add Reference.
  2. Use Add As Link.
  3. 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.
  4. Done. Rebuild and test.

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.




回答2:


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).

You can edit the project's .csproj and link the native dll conditionally:

 <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 PreserveNewest which means "copy if newer".




回答3:


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

You still need the Reference to the managed assembly, 'Fann.Net.dll'




回答4:


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)

  1. Create Folder Dependencies in your Solution Explorer
  2. Add/Existing Item, instead of Add Reference.
  3. Use Add As Link.
  4. In the property of the library in VS, set Build Action to Content and Copy to Output Directory to 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



回答5:


If the dll is not in the project bin file, you should allow the dll to be copied.

  1. Right click on your dll

  2. Click properties

  3. If the Copy to Output Directory is Do not copy, select Copy always

  4. Rebuild the project. It will appear.




回答6:


  1. Right click on your project
  2. Choose Add, then Reference...
  3. In the Reference Manager window click on Browse... (located at the bottom).
  4. Locate your .dll and then press Add.


来源:https://stackoverflow.com/questions/16420595/how-to-add-external-native-dependency-dll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!