How can I add a .dll
in Visual Studio 2010? I just cannot find the option there.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- How to know full paths to DLL's from .csproj f
- Importing NuGet references through a local project
相关文章
- How to show location of errors, references to memb
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- How to track MongoDB requests from a console appli
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
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: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 calllink
for you. You can use the/link
flag to specify additional arguments tolink
and do it all at once if you like: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.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:
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).
On Windows you do not link with a
.dll
file directly – you must use the accompanying.lib
file instead. To do that go toProject -> 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 inOutput Directory
(by default, this isDebug\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.