ILya
ILya

Reputation: 2778

Dynamic linking in Visual Studio

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

Upvotes: 3

Views: 17735

Answers (5)

Sweekritee Singh
Sweekritee Singh

Reputation: 182

If you are calling a method from the dll, you can use explict dynamic linking method.

Mistake: you are including a header evp.h from OpenSSL distribution dll to your prohject

As you are linking dynamically, no need to include .h from A DLL to your project.

  • You can call by following method:
    1. LoadLibrary("libeay32.dll"); /* syntax: */
    2. Declare a function pointer pointing to the function you want to call.

For eg.

Let your libeay32.dll has an exported function: int add(int x, int y);

Then to call it in your project declare a function pointer and then call the method add as follows:

    typedef int (*AddfnPtr)(int num1, int num2);
    int num1 = 2, num2 = 3 ;

    HMODULE handle = NULL;
    handle = LoadLibrary("libeay32.dll");

    if (handle != NULL)
    {
        AddfnPtr addfnptr = (AddfnPtr)GetProcAddr(handle, NULL); 
        if (addfnptr != NULL)
        {
            int res = addfnptr(num1,num2);
            cout << "res = "<<res;
        } 
   }

Upvotes: 0

Afriza N. Arief
Afriza N. Arief

Reputation: 7876

  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.

Upvotes: 3

rsjethani
rsjethani

Reputation: 2227

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

Upvotes: 3

Vicky
Vicky

Reputation: 13244

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

Upvotes: 2

AshleysBrain
AshleysBrain

Reputation: 22591

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.

Upvotes: 3

Related Questions