Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying a DLL's dependencies in Visual Studio

How can I set up a project in Visual Studio to copy the third-party DLLs that one of the project's references depends on?

I have a main application project and a class library DLL. The main application references the class library DLL, and the DLL itself references some third-party DLLs. When I compile the main application, it automatically copies the class library DLL to its output directory, but it does not copy the third-party DLLs.

I do not want to add references to the third-party DLLs from the main application project because the main application does not use them, they're only used by the class library.

like image 343
sourcenouveau Avatar asked Jun 25 '09 13:06

sourcenouveau


People also ask

How do I find DLL dependencies?

Yes, dumpbin.exe is very useful to figure out /dependents and /imports . You can also use it on other machines if you copy link.exe along with it and make sure the corresponding x86 Visual C++ Runtime Redistributable ( msvcr120. dll for Visual Studio 2013) is available on the target machine.

Where are DLL files located in Visual Studio project?

C:\Program Files\MyApp\Modules\SomeModule. dll.


Video Answer


2 Answers

You can achieve this with the project properties window. Visual Studio allows you to define events to occur, before, or after building. To get to the project properties window simply right-click on your project in the solution explorer window and click on 'properties'. From the left hand side go to the 'build events' tab.

In the post-build box type in a few copy commands. For example:

copy "$(SolutionDir)mydll.dll" "$(TargetDir)" 

Where $(SolutionDir) and $(TargetDir) are both predefined variables. The standard syntax is as follows:

copy "source directory and file name" "destination directory" 

If you click on the 'edit post build...' button it will bring up a box which has a listing of these predefined variables that you can insert (like $(SolutionDir) and $(TargetDir))

As a side note, this is a useful process for copying other files, such as custom configuration files, images, or any other dependencies your project may have.

like image 141
Brian Avatar answered Sep 21 '22 18:09

Brian


The following fragment works for me:

<Project>   ...   <ItemGroup>     <Content Include="Path\to\dll\dllname.dll">       <CopyToOutputDirectory>Always</CopyToOutputDirectory>     </Content>   </ItemGroup>   ... </Project> 

This works for C#. For native C++ it still copy dll to output folder, but this dependency is not visible in Visual Studio, it should be edited in project file directly.

To test on non-trivial example I tried to run C# project A which depends on native C++ project B. B projects depends on thirdparty native dll C - this dependency is implemented via fragment above in project file. When I build A, C is copied to binary folder.

I tried it in Visual Studio 2010.

like image 34
sergtk Avatar answered Sep 24 '22 18:09

sergtk