164

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sivabudh
  • 31,807
  • 63
  • 162
  • 228

7 Answers7

242
  1. Go to project properties (Alt+F7)
  2. Under Debugging, look to the right
  3. There's an Environment field.
  4. 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%
  5. Hit F5 (debug) again and it should work.
Jesse Good
  • 50,901
  • 14
  • 124
  • 166
sivabudh
  • 31,807
  • 63
  • 162
  • 228
  • 20
    The Environment field takes NAME=VALUE semicolon-delimited pairs. Append to the PATH variable to have the VS Debugger search additional locations for DLLs: PATH=%PATH%;$(ProjectDir)lib – Brent Faust Dec 08 '12 at 04:06
  • 5
    Is there a similar way to do for Release build too? – Narek Nov 22 '17 at 09:50
  • I needed to add the dll path to the $(LibraryPath) variable (or include it in the list under Library Directories on the Config Properties->VC++ Directories setting). – Foster Boondoggle Apr 19 '18 at 17:48
  • 2
    Alt-F7 brings up a temporary dialog box listing the Active Tool Windows and Active Files (Visual Studio 2017 Community). Is there no way to get to project properties though an old-fashioned menu command? I can't seem to find it. – Slipp D. Thompson Sep 03 '19 at 12:05
  • 1
    @SlippD.Thompson Yeah, you're right. But you can achieve the same thing using Alt + p, p instead. – Jenix Sep 14 '19 at 09:16
  • @Jenix Also, right-clicking on the project and choosing Properties works. When I posted my question 2 weeks ago, I was using Visual Studio for the first time in a decade on a time-crunched project. I guess my larger point here is that hotkeys are great for power users, but shouldn't be listed as the primary way to do something for novice/unfamiliar users performing rudimentary tasks— the actual menu or button or contextual menu (in this case) should be used. Hotkeys can be learned later. – Slipp D. Thompson Sep 17 '19 at 07:23
  • 1
    @SlippD.Thompson Ah, reading your comment again, I think I misunderstood what you meant. Yes, I agree. But at least, there are some hotkeys never changed in Visual Studio, and 'Alt + p, p' is one of them. It was there along with 'Alt + F7', which means now something else as you know. Also, 'Alt + p, p' just means 'Project menu > * Properties'. – Jenix Sep 17 '19 at 19:07
  • The "\" sign should be removed after `$(ProjectDir)` (at least in Visual Studio 2019) – M_N1 May 13 '20 at 13:19
  • How can I edit this for premake? – Ak01 Jan 28 '22 at 15:02
  • how do you set multiple path? – jojo_Aero_smith_the_dummy Mar 28 '22 at 06:07
  • For those who did this and were wondering why it didn't change anything in your vcxproj file, it's because it stores these settings in the `.vcxproj.user` file. – tomocafe Jul 27 '22 at 15:24
  • 1
    Is there an equivalent for Visual Studio 2022? – Doug Kimzey Oct 27 '22 at 11:07
11

Go through project properties -> Reference Paths

Then add folder with DLL's

kravits88
  • 12,431
  • 1
  • 51
  • 53
  • 4
    Please don't post identical answers to multiple questions. Post one good answer, then vote/flag to close the other questions as duplicates. If the question is not a duplicate, *tailor your answers to the question.* – durron597 Sep 03 '15 at 03:00
  • 1
    by far the best answer :) – Rathma Oct 23 '17 at 20:17
  • 2
    This doesn't seem to be available in Visual Studio 2015 in a C++ project. Which combination of Visual Studio version and project type supports this option? – Filip Bártek Feb 22 '18 at 17:46
  • 3
    OP's question was about C++ but this answer is for C#. – Jenix Sep 14 '19 at 09:18
  • 1
    I need to use diffrent dlls in debug mode for a specific dll , how can it possible? –  Dec 07 '21 at 06:50
9

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.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
6

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

Community
  • 1
  • 1
Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
6

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

Project Properties

Joma
  • 3,520
  • 1
  • 29
  • 32
  • 1
    I have no idea if it is right to do that, but the IDE gives me that option and it has been useful, I think it's a good option if I want to copy all the necessary files to another folder to distribute your program. Simple spell but effective. Here in this question there are different answers to reach the same end. But I use this option and avoid many headaches. – Joma Jun 04 '19 at 22:48
  • 1
    The other options didn't work for me, only this one, but it seems like the others should be the right way to go. – rjferguson Jun 27 '19 at 22:26
0

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.

dinozaver
  • 117
  • 3
  • 1
    It would be good to add name of the extension into the answer. – Ruli Jan 14 '21 at 12:36
  • I agree, but as i wrote before, i just installed what visual studio recomended. It was about 4GB of data and since i did not know what was truly neccesary i just installed all of them and did not look closer into every item. – dinozaver Jan 19 '21 at 09:19
  • looks like the DLL had a dependency (i.e. requires another DLL) which was not present or at least not on your PATH until you installed VC++. – Adrian W Apr 23 '21 at 09:56
-1

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.