Why the native DLL is not copied to the output dir

2019-01-19 00:44发布

问题:

I have a C# project A which uses a .net wrapper DLL and a native DLL. I add the .net wrapper DLL to the reference list of project A. Since the wrapper DLL only works with the native DLL when they are in the same folder, the native DLL should be copied to the output directory of project A. I achieve this by adding the native DLL as a content file under project A and set its copy action to copy if newer. This is fine.

If a C# project B has direct reference to project A, VS will copy all dependent files used by project A to the output directory of project B. This means the wrapper DLL and the native DLL will be copied to project B's output directory as well. This works fine as well.

Then I have yet another C# project C, which only directly refers to project B, not project A. It is interesting to see that VS will not copy the native DLL to the output directory of project C, which is what I intend to do otherwise when project C uses the functionality of project B and looks for the native DLL to work with the wrapper DLL, it won't find it.

Can someone explain why VS doesn't copy the native DLL to the output directory of project C? What is the mechanism of copying chain-dependent files in VS? Many thanks.

回答1:

Basically reference-chains don't propagate, and it is up to the topmost assembly (the exe, web-site, etc) to ensure that it has everything it needs, either locally or in (for example with managed dlls) the GAC. You will need to add the files to the exe/web-site as "copy to output".



回答2:

Why not just add the native dll as a reference in project A? This will ensure that it'll always be included when other libraries use A.

Edit: Nevermind, this only works if the dll is a COM or .NET component.



回答3:

Because there is not an explicit dependency in the project, the builder does not know that those need to be output with the known binaries. What you can do is create a post-build configuration to copy those DLLs to a build destination upon compilation. You can Google how to do that, here's one of the first results that explains how to do this:

http://visualstudiohacks.com/articles/general/customize-your-project-build-process/