Referencing external dll in Visual Studio 2015 [duplicate] - c++

I developed an application that depends on a DLL file. When I debug my application, the applicationwould complain that:
"This application has failed to start because xxx.dll was not found."
So I have to copy the DLL file into the same directory as my .vcproj file.
Is there a way to set the project to look for the DLL file in (preferably) some relative path or (not preferred) some absolute path?
Similar concept to how we set include and library path in the project settings.
I mean when I debug my application (hitting F5) the above error would pop up.

Go to project properties (Alt+F7)
Under Debugging, look to the right
There's an Environment field.
Add your relative path there (relative to vcproj folder) i.e. ..\some-framework\lib by appending PATH=%PATH%;$(ProjectDir)\some-framework\lib or prepending to the path PATH=C:\some-framework\lib;%PATH%
Hit F5 (debug) again and it should work.

Go through project properties -> Reference Paths
Then add folder with DLL's

The search path that the loader uses when you call LoadLibrary() can be altered by using the SetDllDirectory() function. So you could just call this and add the path to your dependency before you load it.
See also DLL Search Order.

Another possibility would be to set the Working Directory under the debugging options to be the directory that has that DLL.
Edit: I was going to mention using a batch file to start Visual Studio (and set the PATH variable in the batch file). So then did a bit of searching and see that this exact same question was asked not long ago in this post. The answer suggests the batch file option as well as project settings that apparently may do the job (I did not test it).

In your Project properties(Right click on project, click on property button) ▶ Configuration Properties ▶ Build Events ▶ Post Build Events ▶ Command Line.
Edit and add one instruction to command line.
for example copy botan.dll from source path to location where is being executed the program.
copy /Y "$(SolutionDir)ProjectDirs\x64\Botan\lib\botan.dll" "$(TargetDir)"

I had the same problem and my problem had nothing to do with paths. One of my dll-s was written in c++ and it turnes out that if your visual studio doesn't know how to open a dll file it will say that it did not find it. What i did was locate which dll it did not find, than searched for that dll in my directories and opened it in a separate visual studio window. When trying to navigate through Solution explorer of that project, visual studio said that it cannot show what is inside and that i need some extra extensions, so that it can open those files. Surely enough, after installing the recomended extension (in my case something to do with c++) the
"This application has failed to start because xxx.dll was not found."
error miraculously dissapeared.

I know this question had been answered years ago, but for those like me who needed to change where the debugger starts the application, change the command property under Project Properties -> Debugging.

Related

How do i link libstdc++, libwinpthread, and libgcc to my visual studio project? [duplicate]

This question already has answers here:
How do I set the path to a DLL file in Visual Studio?
(7 answers)
Closed 7 years ago.
In Visual Studio 2010, under VC++ Directories > Executable Directories, I have specified the path to glew32d.dll. However, when I run the executable, it still complains.
On the other hand, if I copy the DLL into the local folder and run the executable then, it doesn't complain.
Can someone please tell me how to fix this? Also, why is Visual Studio not recognizing that path?
Update
Scenario: I currently use a template project which I use as a starter code for a lot of my projects. This template depends on glew32d.dll. I usually store all dependent dlls in a common bin folder. I was hoping to reference this folder and Visual studio could read the dlls from there, instead of me having to copy the dlls everytime. What would be a good way to handle this?
Specifying the path to the DLL file in your project's settings does not ensure that your application will find the DLL at run-time. You only told Visual Studio how to find the files it needs. That has nothing to do with how the program finds what it needs, once built.
Placing the DLL file into the same folder as the executable is by far the simplest solution. That's the default search path for dependencies, so you won't need to do anything special if you go that route.
To avoid having to do this manually each time, you can create a Post-Build Event for your project that will automatically copy the DLL into the appropriate directory after a build completes.
Alternatively, you could deploy the DLL to the Windows side-by-side cache, and add a manifest to your application that specifies the location.
I've experienced same problem with same lib, found a solution here on
SO:
Search MSDN for "How to: Set Environment Variables for Projects".
(It's Project>Properties>Configuration Properties>Debugging
"Environment" and "Merge Environment" properties for those who are in
a rush.)
The syntax is NAME=VALUE and macros can be used (for example,
$(OutDir)).
For example, to prepend C:\Windows\Temp to the PATH:
PATH=C:\WINDOWS\Temp;%PATH%
Similarly, to append $(TargetDir)\DLLS to the PATH:
PATH=%PATH%;$(TargetDir)\DLLS
(answered by Multicollinearity here: How do I set a path in visual studio?
try "configuration properties -> debugging -> environment" and set the PATH variable in run-time
To add to Oleg's answer:
I was able to find the DLL at runtime by appending Visual Studio's $(ExecutablePath) to the PATH environment variable in Configuration Properties->Debugging. This macro is exactly what's defined in the Configuration Properties->VC++ Directories->Executable Directories field*, so if you have that setup to point to any DLLs you need, simply adding this to your PATH makes finding the DLLs at runtime easy!
* I actually don't know if the $(ExecutablePath) macro uses the project's Executable Directories setting or the global Property Pages' Executable Directories setting. Since I have all of my libraries that I often use configured through the Property Pages, these directories show up as defaults for any new projects I create.

Linking to DLL via registry key ignored during debugging in Visual Studio

I'm working in VS 2019 on a C++ program in which I want to link into a bunch of 3rd party dlls, which are located in a folder separate from my executable. I don't want to copy all the dlls into the executable folder and have found the following solution.
Just like in the solution, I create a registry entry to define the keys with the information about the executable and the required path information to link to the DLLs:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MyExe.exe
%Path2MyExe%\MyExe.exe (default key)
Path = %Path2DLLs%
The compilation from within VS works fine and when I start the generated executables from outside VS by (a) double clicking or (b) by using the command line, the 3rd party DLLs are found and the program is executed as expected.
However, when I invoke the debugger from within Visual Studio (c), execution is halted right away complaining that DLLs cannot be found.
I have checked the present working directory from within the code using _getcwd and for all options (a-c) it indicates the same directory.
So the question is: why does option (c) - debugging from within VS - not work?!
Cheers, Sebastian
It's probably because the executable that's launched when debugging is a vshost executable, not the generated executable for the project, and so it needs a separate entry in the registry.
If you want to stick with this kind of a build (highly not recommended), the easiest way to debug is to disable the hosting process:
Following the suggestion by drescherjm I tried to prepend the DLL directory to the PATH variable in "Configuration Properties -> Debugging -> Environment" following this solution.
However, this did not work for me. I also added the path directly in the system environment dialogue without success.
What did work in the end is setting the working directory under "Configuration Properties -> Debugging -> Working Directory" to point to the DLL folder. Now all three calling routes (a-c) work as expected.
Thanks to everyone for taking the time to help me out on this.
Sebastian

How can I tell Visual studio where my additional .dll files are?

I have recently switched my IDE to Visual Studio 2019 for C++ projects. I easily followed a tutorial into setting up a new library like SFML into visual studio, and tell it where the additional include and library directories are.
But there is something else that is required for it to work, which are the .dll files. Every page I followed, even the Documentation by the SFML website, it says that they have to be in the same directory as my project. That means I need to copy-paste the 7-8 files into my project directory. This really makes the folder look untidy. I would like to create a new folder and tell Visual Studio where those files are. I tried going doing this
Project -> Properties -> Linker -> Input -> Additional dependencies
Usually, the lines that would work are
sfml-system-d.lib
sfml-window-d.lib
...
I tried doing $(ProjectDir)valid path\ sfml-files.lib but this gives me the linker error, saying that It could not find the file.
If I simply move the .dlls into a folder without doing anything, the code would compile and link fine. But when it runs, Windows gives me a pop-up box with the same error message.
Here is how it currently looks
Looks really messy, I just want to be able to move them into dependencies like how src contains the source files.
How can I achieve this?
As it is now, it works perfectly fine. The issue only occurs when I try to create a new folder.
I hope I have covered the important information required for a good answer, If not please let me know what more I should add
Microsoft Visual Studio 2019
Currently running 64-bit platform with Debug configuration. Hence the -d suffix
You could create a path environment for your specified directory, which is like drescherjm’s suggestion. The steps:
Right-click “This PC” -> “Properties”-> “Advance System settings”
Click “Environment Variables”
In the System Variables group, edit “Path”
Add your directory, for example: ”D:\ SFML-2.5.1\bin”
Restart your visual studio and re-open your project
The easier solution might be to put them in the x64 subdirectory. This allows you to have various builds side by side (x86/x64, debug/release).
Since this x64 directory is where the EXE is located, it is the first directory searched for DLL's. It will take precedence over the Path solution suggested in the other answer. The Path directories are searched last of all.

Visual Studio doesn't recognize opencv: unable to locate DLL [duplicate]

I am trying just a basic program with OpenCV with the following code:
#include "cv.h"
#include "highgui.h"
int main()
{
IplImage* newImg;
newImg = cvLoadImage("~/apple.bmp", 1);
cvNamedWindow("Window", 1);
cvShowImage("Window", newImg);
cvWaitKey(0);
cvDestroyWindow("Window");
cvReleaseImage(&newImg);
return 0;
}
When I run this, I get
The program can't start because
libcxcore200.dll is missing from your
computer. Try reinstalling the
program to fix this problem.
However, I can see this DLL. It exists. I have added the following to the input dependencies for my linker
C:\OpenCV2.0\lib\libcv200.dll.a
C:\OpenCV2.0\lib\libcvaux200.dll.a
C:\OpenCV2.0\lib\libcxcore200.dll.a
C:\OpenCV2.0\lib\libhighgui200.dll.a
What gives? I'm using visual studio 2008.
When I link the .dll files instead of .dll.a files, I get
fatal error LNK1107:invalid or corrupt
file: cannot read at 0x3F8
libcv200.dll
I followed instructions on http://opencv.willowgarage.com/wiki/VisualC%2B%2B_VS2010 but was still stuck on exactly the same problem, so here's how I resolved it.
Fetched MSVC 2010 express edition.
Fetched Win 32 OpenCV 2.2 binaries and installed in default location.
Created new project.
Project setup
Project -> OpenCV_Helloworld Properties...Configuration Properties -> VC++ Directories
Include Directories... add: C:\OpenCV2.2\include\;
Library Directories... add: C:\OpenCV2.2\lib;C:\OpenCV2.2\bin;
Source Directories... add:
C:\OpenCV2.2\modules\calib3d\src;C:\OpenCV2.2\modules\contrib\src;C:\OpenCV2.2\modules\core\src;C:\OpenCV2.2\modules\features2d\src;C:\OpenCV2.2\modules\flann\src;C:\OpenCV2.2\modules\gpu\src;C:\OpenCV2.2\modules\gpu\src;C:\OpenCV2.2\modules\highgui\src;C:\OpenCV2.2\modules\imgproc\src;C:\OpenCV2.2\modules\legacy\src;C:\OpenCV2.2\modules\ml\src;C:\OpenCV2.2\modules\objdetect\src;C:\OpenCV2.2\modules\video\src;
Linker -> Input -> Additional Dependencies...
For Debug Builds... add:
opencv_calib3d220d.lib;opencv_contrib220d.lib;opencv_core220d.lib;opencv_features2d220d.lib;opencv_ffmpeg220d.lib;opencv_flann220d.lib;opencv_gpu220d.lib;opencv_highgui220d.lib;opencv_imgproc220d.lib;opencv_legacy220d.lib;opencv_ml220d.lib;opencv_objdetect220d.lib;opencv_video220d.lib;
At this point I thought I was done, but ran into the problem you described when running the exe in debug mode. The final step is obvious once you see it, select:
Linker -> General ... Set 'Use Library Dependency Inputs' to 'Yes'
Hope this helps.
Just add C:\OpenCV2.0\bin into your PATH environment variable
or
When you install OpenCV,
Choose the option, Add OpenCV to the system PATH for current user which is not default one
Just for your information,after add the "PATH",for my win7 i need to reboot it to get it work.
In Visual Studio 2013 you need to add this to the Environment Variables and then Restart your pc. This is the path where .dll file located in.
I have had numerous problems with opencv and only succeded after a gruesome 4-6 months. This is the last problem I have had, but all of the above didn't work. What worked for me was just copying and pasting the opencv_core2*.dll (and opencv_highgui2*.dll which it will ask for since you included this as well) into the release (or debug folder - I'm assuming. Haven't tested this) folder of your project, where your application file is.
Hope this helps!
You just need to add the folder of the needed dll file (or files) to your system "Environment Variables" in "Path".
Your problem will 100% be resolved. I had this problem too.
just open the bin folder and copy and paste the .dll files to the folder you are working in..it should fix the problem
As to #Marc's answer, I don't think VC uses the path from the OS. Did you add the path to VC's library paths. I usually add the DLLs to the project and copy if newer on the build and that works very well for me.
you can find the opencv_core248 and other dlls in opencv\build\x86\vc12\bin folder. Just copy the dlls you require into system32 folder. And your app should start working in a flash ! Hope it helps.
This might be like resurrecting a dead horse. But just so it's out there, the reason why the answer to these types of questions to simply put dll's into the system32 folder is because that folder is in the os's system path.
It's actually best practice to provide the os with a path link.
With windows 10
open up file explorer
right click on "this pc" and select "properties"
Now in the "Control Panel\System and Security\System" window that comes up, click on "Advanced System Settings" from the left hand panel.
At the bottom of the next window, click on the "Environment Variables" button.
On the next window, there are two panels, the top one is for modifying variables to the current user, and the bottom panel is for modifying variables to the system. On the bottom panel, find the variable "Path" and click it to select it, then click on the "edit" button.
Here you can then create, edit, delete, or update the different paths for the system. For example, to add mingw32-make to the system so you can access that command via command prompt, click new, then paste in the path to the bin. Example path, "D:\Qt\Tools\mingw730_64\bin", no quotation marks nor additional whitespaces.
Click ok on all the windows so that the changes are saved, then reboot your computer for the changes to be loaded.
The ".a" at the end of your DLL files is a problem, and those are there because you didn't use CMAKE to build OpenCV 2.0. Additionally you do not link to the DLL files, you link to the library files, and again, the reason you do not see the correct library files is because you didn't use CMAKE to build OpenCV 2.0. If you want to use OpenCV 2.0 you must build it for it to work correctly in Visual Studio. If you do not want to build it then I would suggest downgrading to OpenCV 1.1pre, it comes pre-built and is much more forgiving in Visual Studio.
Another option (and the one I would recommend) is to abandon OpenCV and go with EmguCV. I have been playing with OpenCV for about a year and things got much easier when I switched to EmguCV because EmguCV works with .NET, so you can use a language like C# that does not come with all the C++ baggage of pointers, header files, and memory allocation problem.
And as for the question of 64bit vs. 32bit, OpenCV does not officially support 64bit. To be on the safe side open your project properties and change the "Platform Target" under the "Build" tab from "Any CPU" to "X86". This should be done any time you do anything with OpenCV, even if you are using a wrapper like EmguCV.
Copy all .dll from /bin in System32
Just copy the .dll files to C:\windows\system32\
No need to do any of that. It is a visual studio error.
just go here:
http://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=31354
and download the appropriate fix for your computer's OS
close visual studio, run the fix and then restart VS
The code should run without any error.

visual studio 2013 c++ add path to dll [duplicate]

I developed an application that depends on a DLL file. When I debug my application, the applicationwould complain that:
"This application has failed to start because xxx.dll was not found."
So I have to copy the DLL file into the same directory as my .vcproj file.
Is there a way to set the project to look for the DLL file in (preferably) some relative path or (not preferred) some absolute path?
Similar concept to how we set include and library path in the project settings.
I mean when I debug my application (hitting F5) the above error would pop up.
Go to project properties (Alt+F7)
Under Debugging, look to the right
There's an Environment field.
Add your relative path there (relative to vcproj folder) i.e. ..\some-framework\lib by appending PATH=%PATH%;$(ProjectDir)\some-framework\lib or prepending to the path PATH=C:\some-framework\lib;%PATH%
Hit F5 (debug) again and it should work.
Go through project properties -> Reference Paths
Then add folder with DLL's
The search path that the loader uses when you call LoadLibrary() can be altered by using the SetDllDirectory() function. So you could just call this and add the path to your dependency before you load it.
See also DLL Search Order.
Another possibility would be to set the Working Directory under the debugging options to be the directory that has that DLL.
Edit: I was going to mention using a batch file to start Visual Studio (and set the PATH variable in the batch file). So then did a bit of searching and see that this exact same question was asked not long ago in this post. The answer suggests the batch file option as well as project settings that apparently may do the job (I did not test it).
In your Project properties(Right click on project, click on property button) ▶ Configuration Properties ▶ Build Events ▶ Post Build Events ▶ Command Line.
Edit and add one instruction to command line.
for example copy botan.dll from source path to location where is being executed the program.
copy /Y "$(SolutionDir)ProjectDirs\x64\Botan\lib\botan.dll" "$(TargetDir)"
I had the same problem and my problem had nothing to do with paths. One of my dll-s was written in c++ and it turnes out that if your visual studio doesn't know how to open a dll file it will say that it did not find it. What i did was locate which dll it did not find, than searched for that dll in my directories and opened it in a separate visual studio window. When trying to navigate through Solution explorer of that project, visual studio said that it cannot show what is inside and that i need some extra extensions, so that it can open those files. Surely enough, after installing the recomended extension (in my case something to do with c++) the
"This application has failed to start because xxx.dll was not found."
error miraculously dissapeared.
I know this question had been answered years ago, but for those like me who needed to change where the debugger starts the application, change the command property under Project Properties -> Debugging.