如何将我的 MS Visual Studio 项目链接到 OpenSSL 静态库?
在开始这个项目之前,我有一种错误的印象,我精通编码和使用 Visual Studio 2010。现在我意识到我只是自满了。
我的平台是开发盒是 64 位 Windows 7,但我需要针对 64 位和 32 位平台进行构建。我正在使用 VS 2010 和 OpenSSL 1.0.0e,不过如果需要的话,我可以使用另一个。我正在尝试创建一个静态库(*.lib),其他开发人员将其包含到更大的项目中。 *.lib 似乎可以编译,但显然事情并不顺利,因为测试程序中断了。
我无法在我的程序中使用 *.dll,这是来自高层的,因此我一直在尝试将我的库静态链接到 libeay32.lib 和 ssleay32.lib。我才刚刚开始,在使用新东西时喜欢慢慢开始,所以我尝试从 SSL 调用的唯一两个函数是 HMAC 和 EVP_sha512:
HMAC(EVP_sha512(), key, keyLen, lpBufferAddr, dwCurBufSize , hashOut, &len);
我承认我可能完全错了,但你已经明白了。老实说,我唯一需要的是加密函数和 HMAC 函数。我可能可以使用其他东西,但我被告知要使用 OpenSSL。不管怎样,继续......
当构建链接到我的库项目的测试程序(该项目又链接到两个 OpenSSL 库)时,我收到以下链接错误:
error LINK2019: unresolved external symbol _HMAC@28 referenced in function "public: int _thiscall etc"
error LINK2019: unresolved external symbol _EVP_sha512@28 referenced in function "public: int _thiscall etc"
我将这两个库都包含在“附加依赖项”中,并且有将其目录包含在“其他库依赖项”中。
谁能帮我解决这个问题吗?我一直在思考这个问题,并得出了一个不幸的结论:我是个傻瓜。我承认,从 OpenSSL 创建静态库对我来说已经够困难的了,而且我什至不相信我在 x64 上成功了。任何帮助和建议将不胜感激。
Before starting on this project, I had the false impression I was proficient in coding and using Visual Studio 2010. I realize now I was under just full of myself.
My platform is development box is 64bit Windows 7, but I need to build for both 64 and 32 bit platforms. I'm using VS 2010 and OpenSSL 1.0.0e, though if need be, I can use another one. I'm trying to create a static library (*.lib) that will be included into a larger project by other developers. The *.lib seems to compile, but obviously things are not all well since the test program breaks.
I cannot use *.dlls with my program, this has come from on high, therefore I've been trying to statically link my library to libeay32.lib and ssleay32.lib. I've only just begun and like to start slow when using something new, so the only two functions I'm trying to call from SSL are HMAC and EVP_sha512:
HMAC(EVP_sha512(), key, keyLen, lpBufferAddr, dwCurBufSize, hashOut, &len);
I acknowledge I may be doing it entirely wrong, but there you have it. Honestly, the only thing I need are the crypto functions, and HMAC functions. I could probably use something else, but I've been told to use OpenSSL. Anyway, moving on....
When building the test program that links to my library project (which in turn links to the two OpenSSL libraries), I get the following linking errors:
error LINK2019: unresolved external symbol _HMAC@28 referenced in function "public: int _thiscall etc"
error LINK2019: unresolved external symbol _EVP_sha512@28 referenced in function "public: int _thiscall etc"
I include both libraries in "Additional Dependencies", and have included their directory in "Additional Library Dependencies".
Can anyone help me with this? I've been pouring over this and have come to the unfortunate conclusion I'm a dunce. Creating static libs from OpenSSL was difficult enough for me, I admit, and I'm not even convinced I succeeded with x64. Any help and advice would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OpenSSL 是基于 C 的库,并使用 /Gd 选项进行编译。所有函数都遵循 __cdecl 调用约定。
在您正在使用的函数之前添加 __cdecl。 (请注意,添加 __cdecl 后无需重新编译库,因为库是使用此选项编译的)。
例如,如果您使用 SSL_connect
我认为它会解决您的问题。
在少数情况下,您可能需要将函数声明包装在 extern“C”下。但据我所知,在这种情况下,你不需要这样做。
OpenSSL is C-based library and is compiled with /Gd option. All the functions are with __cdecl calling convention.
Add __cdecl before the functions which you are using. (Note that there is no need to recompile the library after adding __cdecl since the library is compiled with this option).
e.g. if you are using SSL_connect
I think, it will solve your problem.
In few cases, you many need to wrap the function declaration under extern "C". But as far as I know, in this case, you should not need to do this.