Linking dll in Visual Studio
Asked Answered
S

4

59

How can I add a .dll in Visual Studio 2010? I just cannot find the option there.

Sarmentose answered 21/10, 2011 at 6:47 Comment(0)
B
85

On Windows you do not link with a .dll file directly – you must use the accompanying .lib file instead. To do that go to Project -> Properties -> Configuration Properties -> Linker -> Additional Dependencies and add path to your .lib as a next line.

You also must make sure that the .dll file is either in the directory contained by the %PATH% environment variable or that its copy is in Output Directory (by default, this is Debug\Release under your project's folder).

If you don't have access to the .lib file, one alternative is to load the .dll manually during runtime using WINAPI functions such as LoadLibrary and GetProcAddress.

Bellona answered 21/10, 2011 at 6:58 Comment(2)
I've seen places where they mention the .h files too. Do we need them for this process? I think they are not needed, since the .lib file contains all the information for the dll.Equilibrant
@darkgaze I would think they are still needed because the .h files usually contain all of the prototypes, declarations, constants, macros etc.Barbarbarbara
P
38

You don't add or link directly against a DLL, you link against the LIB produced by the DLL.

A LIB provides symbols and other necessary data to either include a library in your code (static linking) or refer to the DLL (dynamic linking).

To link against a LIB, you need to add it to the project Properties -> Linker -> Input -> Additional Dependencies list. All LIB files here will be used in linking. You can also use a pragma like so:

#pragma comment(lib, "dll.lib")

With static linking, the code is included in your executable and there are no runtime dependencies. Dynamic linking requires a DLL with matching name and symbols be available within the search path (which is not just the path or system directory).

Pompidou answered 21/10, 2011 at 6:59 Comment(2)
About static linking: How can i make Visual Studio to copy the dll from a certain directory to the output on build (or on linking)?Whipstitch
@Whipstitch Each project provides pre-build, post-build, and a variety of other steps (check in the project settings). You can do a file copy there, or even run an entire script (I have a script set up to create a header from git revision IDs and increment build number in pre-build, then copy files to a testing install in post).Pompidou
S
7

I find it useful to understand the underlying tools. These are cl.exe (compiler) and link.exe (linker). You need to tell the compiler the signatures of the functions you want to call in the dynamic library (by including the library's header) and you need to tell the linker what the library is called and how to call it (by including the "implib" or import library).

This is roughly the same process gcc uses for linking to dynamic libraries on *nix, only the library object file differs.

Knowing the underlying tools means you can more quickly find the appropriate settings in the IDE and allows you to check that the commandlines generated are correct.

Example

Say A.exe depends B.dll. You need to include B's header in A.cpp (#include "B.h") then compile and link with B.lib:

cl A.cpp /c /EHsc
link A.obj B.lib

The first line generates A.obj, the second generates A.exe. The /c flag tells cl not to link and /EHsc specifies what kind of C++ exception handling the binary should use (there's no default, so you have to specify something).

If you don't specify /c cl will call link for you. You can use the /link flag to specify additional arguments to link and do it all at once if you like:

cl A.cpp /EHsc /link B.lib

If B.lib is not on the INCLUDE path you can give a relative or absolute path to it or add its parent directory to your include path with the /I flag.

If you're calling from cygwin (as I do) replace the forward slashes with dashes.

If you write #pragma comment(lib, "B.lib") in A.cpp you're just telling the compiler to leave a comment in A.obj telling the linker to link to B.lib. It's equivalent to specifying B.lib on the link commandline.

Shoshana answered 10/10, 2018 at 13:54 Comment(0)
G
0

Assume that the source file you want to compile is main.cpp and your example_dll.dll and example_dll.lib . now run cl.exe main.cpp /EHsc /link example_dll.lib now you may get main.exe

Gameness answered 8/6, 2020 at 5:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.