Dynamic linking in Visual Studio

14,319

Solution 1

In the project properties, configuration properties, linker, input - add the library name under "additional dependencies".

[Note, this will actually STATICALLY link with the library. If you truly want to load the library dynamically you will need to call LoadLibrary() on the DLL and then get function pointers for the functions you need using GetProcAddress().

See for example

http://msdn.microsoft.com/en-us/library/ms886736.aspx

and

http://msdn.microsoft.com/en-us/library/ms885634.aspx

Solution 2

There is probably a .lib file you also need to add to your compiler's linker input. Check the documentation for the library you're using.

Solution 3

Try using Win32 API's LoadLibrary function, the following link might be of some help :example

Solution 4

  1. if your application need to be able to run without the existence of OpenSSL, use dynamic linking with explicit run-time linking and handle the cases when the DLLs are not around (e.g. by changing your application's behavior / switching to other libraries).
    I recently found a nice examples on this:
  2. if your application may only run if the OpenSSL exist in the environment (or you ship the DLL), use implicit run-time linking.
    For MSVC, the simplest is to add #pragma comment(lib,"libeay32.lib") in your source code (You will probably need the .lib stub to be produced by the same compiler you use)
  3. if your application need to be independent of the environment. Link OpenSSL statically (also uses .lib).

Note that there are 2 kinds of .lib. The first is used for dynamic but implicit linking, second is for static linking. The one for dynamic implicit linking contains stubs that load the DLL for you whereas the one for static linking contain the actual implementation.

Share:
14,319
ILya
Author by

ILya

Updated on June 28, 2022

Comments

  • ILya
    ILya almost 2 years

    I have to link dynamically with OpenSSL libeay32.dll. I'm writing native c++ console application using Visual C++ Express 2008.

    I'm including a header evp.h from OpenSSL distribution. Building and...:

    error LNK2001: unresolved external symbol _EVP_aes_256_cbc
    error LNK2001: unresolved external symbol _EVP_DecryptInit
    error LNK2001: unresolved external symbol _EVP_CIPHER_CTX_init
    

    How to make calls to libeay32.dll methods? I don't know where to specify it's filename

  • Matti
    Matti about 14 years
    That's not really static linking... The actual symbol lookup and making sure callers point to callees still happens dynamically at runtime.
  • ILya
    ILya about 14 years
    May i have another two questions? 1. I have a dll project in my solution. To make calls to it available i do the following. Right click on my exe project -> properties -> Common Properties -> Framework & References -> Add New Reference -> in the opened window i choose my dll project from list. An important remark that it's not managed assemblies. All is native. Then i include a header file in my exe project, build and all is ok... How exactly VS behaves in this case? Can i link to libeay32.dll the same way? There is no option to browse to dll...
  • ILya
    ILya about 14 years
    2. After using .lib to link can i simply replace a libeay32.dll to update it. Or i'll have t ore-link my project?