I set up a c++ project to use a DLL in visual studio. The library had .h .lib and .dll files. The program compiles and runs, and successfully uses the library. The DLL was put in the root directory of the source, not the output directory. When I build, I expected the DLL to be copied form the source to the output directory, but it's not. All I see in the output directory is 3 files of type, application, incremental linker file, and program debug database.
Maybe I'm wrong but I thought that the DLL needed to be in the same folder as the executable in the output directory. So what's going on?
This is something I had difficulty finding as well, but VS does not copy the .dll's for you. You have to tell it to by creating a Post-Build event. Check out the link on XCOPY:
https://support.microsoft.com/en-us/kb/240268
I needed to copy some dll's to my target directory (the build directory) so used the following in a Post-Build event in my project settings:
xcopy $(ProjectDir)openal32.dll $(TargetDir) /Y /D
Just replace "openal32.dll" with whatever dll you need to copy and make a new build event for each dll you need copied to the target (build) directory.
/Y suppresses prompting to confirm overwrite
/D instructs the compiler to only copy if it's a newer version of the dll on this build.
When I build, I expected the DLL to be copied form the source to the
output directory, but it's not.
Maybe I'm wrong but I thought that the DLL needed to be in the same
folder as the executable in the output directory. So what's going on?
First, DLL's are not part of the build process, that's one reason why Visual Studio doesn't do anything with your DLL.
Having said that, another reason why Visual Studio shouldn't automatically copy over the DLL to your executable directory is that Visual Studio doesn't and cannot assume this is where you want your DLL placed.
It can't assume this because you may want your DLL to be placed in one of the directories that will be found by the Windows OS when it is time to load the DLL. See this link:
Dynamic Link Library Search Order
What if you are testing your app to see if it will load the DLL correctly if it is placed in a certain directory on your PATH? How will you do that if Visual Studio were to always copy the DLL to your executable directory?
Another scenario: What if your program uses LoadLibrary
and wants to test if the program behaves correctly if the DLL is not found?
As the other answer suggested, that's why you have to make the concerted effort of telling Visual Studio what to do in the post build event. Also, even after applying the post build event, Visual Studio still knows absolutely nothing about your DLL -- all it sees is a copy
or xcopy
command.