I'm currently developing a C++ app with SDL, which requires DLLs to work. Right now, it will only find DLLs in the same folder as the program. How can I make the program search in a sub-directory named "libs".
-
why not make a shortcut? – Bartlomiej Lewandowski Dec 22 '13 at 20:05
-
Why not copy the DLL to `C:\WINDOWS\system32`? – Kerrek SB Dec 22 '13 at 20:12
-
@KerrekSB, might as well copy it to your PATH then. – ManuelH Dec 22 '13 at 20:16
-
I want this to work if I distrubeted this exe file, it would look for the dlls in the sub folder. – tVoss42 Dec 22 '13 at 20:43
-
@KerrekSB Because the system owns that directory and you are meant to leave it alone. Please don't suggest anyone writes there – David Heffernan Dec 22 '13 at 20:49
2 Answers
If you use load time linking then you need for the DLL to be located in the DLL search path. That is documented here: Dynamic-Link Library Search Order. Typically this would require that you add the DLL folder to the PATH
environment variable. Now, adding a folder to the PATH
environment variable is heavy weight solution to a problem. You surely don't want to do that.
On the other hand, if you switch to run time linking then you can pass the full path of your DLL to LoadLibrary
. You can call GetModuleFileName
to find the file name of the executable, and then pull out the directory, and add \libs\MyDll.dll
. But the big downside of run time linking is that you need to use GetProcAddress
for each function that you import.
Neither of these options is particularly attractive. My advice would be to change your proposed design. Put all the DLLs that the executable needs into the same directory as the executable.

- 601,492
- 42
- 1,072
- 1,490
Add the dll folder path in the Environment Variables
(be careful that the paths in there are separated by ;
).

- 49,413
- 29
- 133
- 174
-
I want this to be a universal setting, so that even if I distributed the files, the program would still know where to look for the dlls. – tVoss42 Dec 22 '13 at 20:44
-
@tVoss42 This will be a universal setting, as least for your PC. – herohuyongtao Dec 22 '13 at 20:47