How do I use a third-party DLL file in Visual Studio C++?
Asked Answered
E

6

84

I understand that I need to use LoadLibrary(). But what other steps do I need to take in order to use a third-party DLL file?

I simply jumped into C++ and this is the only part that I do not get (as a Java programmer). I am just looking into how I can use a Qt Library and tesseract-ocr, yet the process makes no sense to me and is so difficult to google.

How do I tell the compiler of the functions that I am using? Should there be an include file from the third-party vendor?

Extrorse answered 30/1, 2009 at 15:0 Comment(1)
Do you understand how to add third-party dll file in visual studio 2012 for C++. Please help me with this.Misshapen
E
79

As everyone else says, LoadLibrary is the hard way to do it, and is hardly ever necessary.

The DLL should have come with a .lib file for linking, and one or more header files to #include into your sources. The header files will define the classes and function prototypes that you can use from the DLL. You will need this even if you use LoadLibrary.

To link with the library, you might have to add the .lib file to the project configuration under Linker/Input/Additional Dependencies.

Eavesdrop answered 30/1, 2009 at 15:26 Comment(8)
Thanks, the other answers were very good (and useful for me as well) but this explained exactly what I was looking for.Extrorse
The 'additional dependencies' dialogue looks like it only supports filenames and extensions, not file paths. Adding the filename + extension didn't fix 'identifier not found' errors. Adding the fully qualified path + filename + didn't fix them either.Inerasable
@advocate, there's an entry for "Additional Library Directories" on the "General" tab. I have no idea why Microsoft separated them.Eavesdrop
I actually did this, but I still get Linker errors. I've put my .dll file into my C:/Windows. What am I doing wrong?Lan
@anon58192932 Visual Studio allows full library paths in the 'Additional Dependencies' parameter.Viviennevivify
This is the first place where I read that I have to add the .h files to include. Every other tutorial says you only need the .lib and the .dll. Can somebody please explain this? . Here you have another option: #7846386Vulpecula
@darkgaze you can use #import to automatically generate and include a header file for a DLL. The compiler needs to know the declaration of all the functions so that it can build the calling code.Eavesdrop
If there's no .h file which is the human readable reference for all objects and methods, how can you create instance of the objects or call the methods from the library?Stedman
V
77

To incorporate third-party DLLs into my VS 2008 C++ project I did the following (you should be able to translate into 2010, 2012 etc.)...

I put the header files in my solution with my other header files, made changes to my code to call the DLLs' functions (otherwise why would we do all this?). :^) Then I changed the build to link the LIB code into my EXE, to copy the DLLs into place, and to clean them up when I did a 'clean' - I explain these changes below.

Suppose you have 2 third-party DLLs, A.DLL and B.DLL, and you have a stub LIB file for each (A.LIB and B.LIB) and header files (A.H and B.H).

  • Create a "lib" directory under your solution directory, e.g. using Windows Explorer.
  • Copy your third-party .LIB and .DLL files into this directory

(You'll have to make the next set of changes once for each source build target that you use (Debug, Release).)

  1. Make your EXE dependent on the LIB files

    • Go to Configuration Properties -> Linker -> Input -> Additional Dependencies, and list your .LIB files there one at a time, separated by spaces: A.LIB B.LIB
    • Go to Configuration Properties -> General -> Additional Library Directories, and add your "lib" directory to any you have there already. Entries are separated by semicolons. For example, if you already had $(SolutionDir)fodder there, you change it to $(SolutionDir)fodder;$(SolutionDir)lib to add "lib".
  2. Force the DLLs to get copied to the output directory

    • Go to Configuration Properties -> Build Events -> Post-Build Event
    • Put the following in for Command Line (for the switch meanings, see "XCOPY /?" in a DOS window):

    XCOPY "$(SolutionDir)"\lib\*.DLL "$(TargetDir)" /D /K /Y

    • You can put something like this for Description:

    Copy DLLs to Target Directory

    • Excluded From Build should be No. Click OK.
  3. Tell VS to clean up the DLLs when it cleans up an output folder:

    • Go to Configuration Properties -> General -> Extensions to Delete on Clean, and click on "..."; add *.dll to the end of the list and click OK.
Vrablik answered 5/12, 2013 at 21:11 Comment(6)
I am trying to use your method to use third-party dll's. I am using Visual studio 2012 ultimate and It do not have Additional Library Directories in Configuration Properties -> General tab. Please help me.Misshapen
That should be Configuration Properties -> Linker -> General -> Additional Library Directories.Festschrift
@Misshapen you can find that in Configuration Properties > VC++ Directories > Library Directories. It's a generation shift ;)Dogface
A quote is miss placed for step 2. Use: XCOPY "$(SolutionDir)\lib\*.DLL" "$(TargetDir)" /D /K /YConvert
@TristanCHARBONNIER Interesting. I'm pretty sure I copied/pasted my XCOPY command from my working configuration. Are you saying the quote is misplaced because it didn't work for you, or is that just speculation?Vrablik
Thank you, point 2 solved I problem I had with the program not being able to find the .dll at runtime, although it compiled fine!Exposition
O
26

These are two ways of using a DLL file in Windows:

  1. There is a stub library (.lib) with associated header files. When you link your executable with the lib-file it will automatically load the DLL file when starting the program.

  2. Loading the DLL manually. This is typically what you want to do if you are developing a plugin system where there are many DLL files implementing a common interface. Check out the documentation for LoadLibrary and GetProcAddress for more information on this.

For Qt I would suspect there are headers and a static library available that you can include and link in your project.

Orientate answered 30/1, 2009 at 15:8 Comment(4)
And there's a third way, similar to your first way, using the /MT switch, which will statically link your libraries. The advantage is that there is no dynamic linking overhead. However, this cannot be used if you also use the /clr switch.Lucilucia
That only applies to the standard library. For custom Libraries (third party) you can't link a dll statically unless someone explicitly built a static version of it that you can link.Orientate
@Orientate I'm developing a DLL project but when i build it, it's just creating the dll and lib files. it's not creating an includes directory. how can i include my header files in the project ?Homolographic
The dll/import library are the outputs of the linker. When it comes to providing the includes you need to make sure it all gets distributed together. How you do this is up to your build system but if using cmake, look at the install family of commands which will help you decide up the important files for distribution in a structured wayOrientate
U
8

In order to use Qt with dynamic linking you have to specify the lib files (usually qtmaind.lib, QtCored4.lib and QtGuid4.lib for the "Debug" configration) in
Properties » Linker » Input » Additional Dependencies.

You also have to specify the path where the libs are, namely in
Properties » Linker » General » Additional Library Directories.

And you need to make the corresponding .dlls are accessible at runtime, by either storing them in the same folder as your .exe or in a folder that is on your path.

Unthoughtof answered 30/1, 2009 at 15:22 Comment(0)
S
3

I'f you're suppsed to be able to use it, then 3rd-party library should have a *.lib file as well as a *.dll file. You simply need to add the *.lib to the list of input file in your project's 'Linker' options.

This *.lib file isn't necessarily a 'static' library (which contains code): instead a *.lib can be just a file that links your executable to the DLL.

Severus answered 30/1, 2009 at 15:8 Comment(0)
K
3

You only need to use LoadLibrary if you want to late bind and only resolve the imported functions at runtime. The easiest way to use a third party dll is to link against a .lib.


In reply to your edit:

Yes, the third party API should consist of a dll and/or a lib that contain the implementation and header files that declares the required types. You need to know the type definitions whichever method you use - for LoadLibrary you'll need to define function pointers, so you could just as easily write your own header file instead. Basically, you only need to use LoadLibrary if you want late binding. One valid reason for this would be if you aren't sure if the dll will be available on the target PC.

Katonah answered 30/1, 2009 at 15:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.