visual-studio Visual Studio 中的动态链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2630695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamic linking in Visual Studio
提问by ILya
I have to link dynamically with OpenSSL libeay32.dll. I'm writing native c++ console application using Visual C++ Express 2008.
我必须与 OpenSSL libeay32.dll 动态链接。我正在使用 Visual C++ Express 2008 编写本机 C++ 控制台应用程序。
I'm including a header evp.h from OpenSSL distribution. Building and...:
我包含来自 OpenSSL 发行版的标头 evp.h。建筑和...:
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
如何调用 libeay32.dll 方法?我不知道在哪里指定它的文件名
采纳答案by Vicky
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().
[注意,这实际上会与库静态链接。如果您真的想动态加载库,您将需要在 DLL 上调用 LoadLibrary(),然后使用 GetProcAddress() 获取您需要的函数的函数指针。
See for example
参见例如
http://msdn.microsoft.com/en-us/library/ms886736.aspx
http://msdn.microsoft.com/en-us/library/ms886736.aspx
and
和
回答by AshleysBrain
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.
您可能还需要将一个 .lib 文件添加到编译器的链接器输入中。检查您正在使用的库的文档。
回答by rsjethani
回答by Afriza N. Arief
- if your application need to be able to run without the existence of OpenSSL, use dynamic linking with explicit run-time linkingand 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: - 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) - if your application need to be independent of the environment. Link OpenSSL statically (also uses .lib).
- 如果您的应用程序需要能够在不存在 OpenSSL的情况下运行,请使用带有显式运行时链接的动态链接并处理 DLL 不在的情况(例如,通过更改应用程序的行为/切换到其他库)。
我最近发现了一个很好的例子: - 如果您的应用程序只能在环境中存在 OpenSSL(或您提供 DLL)时运行,请使用隐式运行时链接。
对于 MSVC,最简单的方法是添加#pragma comment(lib,"libeay32.lib")源代码(您可能需要使用相同的编译器生成 .lib 存根) - 如果您的应用程序需要独立于环境。静态链接 OpenSSL(也使用 .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.
请注意,有 2 种.lib. 第一个用于动态但隐式链接,第二个用于静态链接。动态隐式链接包含为您加载 DLL 的存根,而静态链接包含实际实现。
回答by Sweekritee Singh
If you are calling a method from the dll, you can use explict dynamic linking method.
如果从 dll 调用方法,则可以使用显式动态链接方法。
Mistake: you are including a header evp.h from OpenSSL distribution dll to your prohject
错误:您将 OpenSSL 分发 dll 中的标头 evp.h 包含到您的项目中
As you are linking dynamically, no need to include .h from A DLL to your project.
当您动态链接时,无需将 .h 从 A DLL 包含到您的项目中。
- You can call by following method:
- LoadLibrary("libeay32.dll"); /* syntax: */
- Declare a function pointer pointing to the function you want to call.
- 您可以通过以下方法调用:
- LoadLibrary("libeay32.dll"); /* 句法: */
- 声明一个指向要调用的函数的函数指针。
For eg.
例如。
Let your libeay32.dll has an exported function: int add(int x, int y);
让你的 libeay32.dll 有一个导出函数: 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:
然后在你的项目中调用它,声明一个函数指针,然后调用 add 方法,如下所示:
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;
}
}

