Windows下OpenSSL安装配置

来源:互联网 发布:db2和mysql sql语句 编辑:程序博客网 时间:2024/05/29 05:11

最近用到openssl,以前从来没用过,所以感觉特陌生,从下载安装、到编译通过,断断续续搞了两天时间,网上关于openssl的文章也很多,但可能很难找到一篇能完全解决自己的问题的,最终也是东拼西凑才终于通过。所以赶紧整理一下,或许后来者也能悟出点道道来。openssl官网文档也很全面...遇到问题特别建议先看看官网FAQ以及openssl目录下的install.w32(install.w64)文件(直接拉到浏览器)。

本文系统环境:Windows XP SP2+Visual Studio 2005+Perl 5.8.6+OpenSSL 0.9.8。

一、预安装准备

1、安装Visual Studio 2005

如果安装的是VC++60,实际在编译OpenSSL时,总是会缺少某些文件,比如ml.exe,cl.exe....还有masm错误等,头都大了,还等着要测试,没时间一点点去试,直接换成Visual Studio 2005了。

2、安装Perl

下载地址:http://www.activestate.com/ActivePerl

天缘安装的ActivePerl-5.8.6.811-MSWin32-x86-122208.msi版本,太高版本没用,以前做手机软件时一直对这个版本倾爱有加。默认安装目录C:\Perl。

3、下载OpenSSL 0.9.8

下载地址:http://www.openssl.org/source/,版本不要太新(如果是做产品开发建议用最新的,前期测试所有软件都可以用老一点的,会少一些兼容问题,新版本往往跨度较大,修改较多,多个软件配合时自然很容易打架)。

如果只是使用OPENSSL,而且版本合适,可以尝试人家编译好了的,直接下载安装即可:

http://www.openssl.org/related/binaries.html

二、安装OpenSSL

1、解压OpenSSL到系统盘

比如C:\OpenSSL。

2、配置WIN32环境

打开CMD命令行,进入C:\OpenSSL目录,执行命令

perl Configure VC-WIN32

3、进入VC BIN目录,配置VC环境变量

进入VS安装路径VC/Bin目录下,运行:

VCVARS32.BAT

设置环境变量。

4、返回OpenSSL目录,创建makefile文件

ms\do_ms

该命令不执行汇编语言编译,如报告文末错误,可以尝试ms\do_masm(使用汇编语言)、ms\do_nasm、ms\do_nt等,这几个配置文件是针对不同的系统配置写的批处理。

至于这几个命令区别如下,其它的暂时没用到未深究,有经验之士欢迎赐教。

- If you are using MASM then run:

> ms\do_masm

- If you are using NASM then run:

> ms\do_nasm

- If you don't want to use the assembly language files at all then run:

> ms\do_ms

5、在Openssl目录下,执行编译

nmake -f ms\ntdll.mak

最终编译动态库完成后,输出都在out32dll目录下:包括可执行文件、两个dll 和两个lib文件: libeay32.dll, libeay32.lib, ssleay32.dll, ssleay32.lib,如果使用VS/VC编程只需按照下文的方法进行即可,如果需要使用openssl命令,还需要在系统环境变量path中增加c:\openssl\out32dll路径,因为openssl.exe就在该目录下,声明后可以直接在命令行中使用openssl命令。

三、编程设置

1、 配置包含文件

打开VS->Tools->Options->projects and solutions->VC++ Directories(VC6路径VC->Tools->Options->Directories),在Include files中增加C:\openssl\inc32目录,假设openssl安装目录为C:\openssl,下同。

在Libray files中增加C:\openssl\out32dll。

2、 包含头文件,导入LIB库

在源程序中包含以下三个头文件并加入LIB:

#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#pragma comment(lib, "libeay32.lib")

#pragma comment(lib, "ssleay32.lib")

3、 更多OpenSSL编程参考:

四、常见错误及解决方法

1、不认识NMAKE?

解决办法A:http://www.question-defense.com/2009/03/27/nmake-is-not-recognized-as-an-internal-or-external-command-operable-program-or-batch-file

解决办法B:使用VS2005的控制台,开始菜单->Microsoft Visual Studio 2005->Visual Studio Tools->Visual Studio 2005 Command Prompt。NMAKE目录实际在C:\Program Files\Microsoft Visual Studio 8\VC\bin下。

2、NMAKE编译fatal error?

NMAKE : fatal error U1077: 'cl' : return code '0x2'

NMAKE : fatal error U1077: 'ml' : return code '0x1'

NMAKE : fatal error U1077: 'link' : return code '0x460'

遇到以上错误,请参考:http://www.flatws.cn/article/program/perl/2011-05-19/26439.html

3、NMAKE编译遇到message Error?

如果遇到:

Message: 'The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _read. See online help for details.' NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 8\VC\BIN\cl.EXE"' : return code '0x2'

Stop.

错误,打开ms\ntdll.mak,把CFLAG= /MD /W3 /WX....里的/WX标志去掉。

4、_md5_block_asm_host_order编译错误?

如果出现下面错误,可以尝试使用ms\do_masm编译命令。

link /nologo /subsystem:console /machine:I386 /opt:ref /dll /out:out32 dll\libeay32.dll /def:ms/LIBEAY32.def @C:\DOCUME~1\Test\LOCALS~1\Temp\nm58C.tmp ms/LIBEAY32.def(7) : warning LNK4017: DESCRIPTION statement not supported for the target platform; ignored
Creating library out32dll\libeay32.lib and object out32dll\libeay32.exp
md5_dgst.obj : error LNK2019: unresolved external symbol _md5_block_asm_host_order referenced in function _MD5_Update
out32dll\libeay32.dll : fatal error LNK1120: 1 unresolved externals
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 8\VC\BIN\link.EXE"' : return code '0x460'
Stop.

参考资料:

1、cnblogs(如何在Windows下编译OpenSSL )

2、51cto(在Windows下编译OpenSSL(VS2005))

0 0
原创粉丝点击