Where to place and how to include dll files in c++ project?

11,149

There's nothing wrong with this approach, and it's probably the quickest way to get your application up and running. Your application needs these libraries, so putting them in the same folder allows it to find them.

There are of course always alternatives!

Static linking

To avoid having a bunch of dlls that have to exist with your application, you could use static linking. You link the .lib files at link-time rather than linking to the .dll files at run-time. Makes your .exe larger, but means you may be able to distribute a single file.

Placing Dlls in a different folder

The dlls do not have to be in the same folder as the .exe, though that normally makes the most sense. Windows will search several folders when looking for a .dll at run-time, so you could put the dlls in the current directory, a system directory (definitely not recommended), or another directory in your PATH environment variable. Actually none of those locations are recommended! Putting them in the same folder as the .exe is the safest, because generally you have control of that folder.

The specific rules for how Windows searches for a .dll are outlined here: http://msdn.microsoft.com/en-ca/library/windows/desktop/ms682586(v=vs.85).aspx

Custom Build Step

I don't like manually putting files in my debug or release folders. I like to think of the debug folder as something I can blow away and rebuild at any time, and I like to have a one-step build process that puts everything where it needs to be so I can easily build on any machine. So I will usually create a custom build step that copies the required .dlls from a "source" folder (in source control) into my output folder.

Share:
11,149

Related videos on Youtube

AturSams
Author by

AturSams

Indie Dev

Updated on July 28, 2022

Comments

  • AturSams
    AturSams over 1 year

    I read this guide that walks you through the steps necessary to create a "visual" application with Cairo and Visual C++. The guide suggests you download certain dll files and store them in the directory in which your executable is created (debug). Here is the list of the files Nil is referring to in his tutorial:

    • cairo Binaries (yes you need the binaries package too, as the Dev one doesn't contain the DLL) -> libcairo-2.dll
    • zlib Binaries -> zlib1.dll
    • libpng Binaries -> libpng12-0.dll
    • Freetype Binaries -> freetype6.dll
    • FontConfig Binaries -> libfontconfig-1.dll
    • expat Binaries -> libexpat-1.dll

    As you can see, it's quite a lot of files. I've been wondering if this the "correct" way of doing this? Is there an alternative way that is considered "best-practice" in this case?