Visual Studio 2019编译 OpenSSL x86/64 static/dynamic

kyaa111 3月前 ⋅ 177 阅读

最近有需要, 需要openssl支持

1. 下载源码

我下的是openssl-1.0.1o, https://www.openssl.org/source/old/

2. 安装Perl

https://strawberryperl.com/download/5.26.3.1/strawberry-perl-5.26.3.1-64bit-portable.zip

perl/bin 加个环境变量

3. 编译安装

perl Configure VC-WIN32 no-asm

VC-WIN64I代表目标是64位库,VC-WIN32 代表是32位库,no-asm 编译过程中不使用汇编代码加快编译过程

VC-WIN32(32位) | VC-WIN64A(64位AMD) | VC-WIN64I(64位Intel) | VC-CE(Windows CE)

prefix 为安装目录

ms\do_ms

64位用这个ms\do_win64a

nmake -f ms\nt.mak

nmake -f ms\ntdll.mak (ntdll 代表动态库,nt 代表静态库)

运行后提示这个

cl: 命令行 warning D9035 :“O”选项已否决,并将在将来的版本中移除
o_str.c
.\crypto\o_str.c(67): fatal error C1083: 无法打开包括文件: “strings.h”: No such file or directory

定位到这个文件

#if !defined(OPENSSL_IMPLEMENTS_strncasecmp) && \
    !defined(OPENSSL_SYSNAME_WIN32) && \
    !defined(NETWARE_CLIB)
# include <strings.h>
#endif

ms/nt.mak内, 在CFLAG=后加上-DOPENSSL_SYSNAME_WIN32 -DWIN32_LEAN_AND_MEAN 即可

继续运行nmake -f ms\nt.mak

又报错

constant_time_test.c
        link /nologo /subsystem:console /opt:ref /debug /out:out32\constant_time_test.exe @C:\Users\root\AppData\Local\Temp\nm8146.tmp
constant_time_test.obj : error LNK2019: 无法解析的外部符号 ___iob_func,函数 _main 中引用了该符 号
out32\constant_time_test.exe : fatal error LNK1120: 1 个无法解析的外部命令

查看代码文件e_os.h 318行存在如下定义

#    if _MSC_VER>=1300
#     undef stdin
#     undef stdout
#     undef stderr
FILE *__iob_func();
#     define stdin  (&__iob_func()[0])
#     define stdout (&__iob_func()[1])
#     define stderr (&__iob_func()[2])
#    elif defined(I_CAN_LIVE_WITH_LNK4049)

__iob_func在VS2019没有了

实际上VS已经定义了宏stdin,stdio,stderr,直接使用VS定义也是可以的。

VS2019中对stdin,stdio,stderr的定义如下:

_ACRTIMP_ALT FILE* __cdecl __acrt_iob_func(unsigned);

#define stdin (__acrt_iob_func(0))
#define stdout (__acrt_iob_func(1))
#define stderr (__acrt_iob_func(2))

将openssl 源码目录删掉, 以上步骤重来一遍, 直到nmake -f ms\nt.mak前, 更改对应文件

为了保持openssl代码的一致,修改e_os.h 318代码如下:

#    if _MSC_VER>=1900
#     undef stdin
#     undef stdout
#     undef stderr
FILE*  __acrt_iob_func(unsigned);
#     define stdin  (__acrt_iob_func(0))
#     define stdout (__acrt_iob_func(1))
#     define stderr (__acrt_iob_func(2))
#    elif _MSC_VER>=1300 && _MSC_VER<1900
#     undef stdin
#     undef stdout
#     undef stderr

或者直接使用VS对宏的定义

#    if _MSC_VER>=1300 && _MSC_VER<1900
#     undef stdin
#     undef stdout
#     undef stderr

nmake -f ms\nt.mak install

nmake -f ms\nt.mak test

同样注意 nt/ntdll

最终输出

passed all tests

即编译安装完成

最终安装在了

D:\usr\local\ssl\lib


1.0.2

perl Configure VC-WIN32 no-asm no-shared --prefix=d:\openssl_lib

ms\do_ms

nmake -f ms\nt.mak

nmake -f ms\nt.mak install

32位release模式的静态库

perl Configure VC-WIN32 no-asm --prefix = "d:\openssl-101\32releaseLib"
ms\do_ms
nmake -f ms\nt.mak
nmake -f ms\nt.mak install
nmake -f ms\nt.mak test

32位debug模式的静态库

perl Configure debug-VC-WIN32 no-asm --prefix = "d:\openssl-101\32debugLib"
ms\do_ms
nmake -f ms\nt.mak
nmake -f ms\nt.mak install
nmake -f ms\nt.mak test
 

32位release模式的动态库

perl Configure VC-WIN32 no-asm --prefix = "d:\openssl-101\32releaseDll"
ms\do_ms
nmake -f ms\ntdll.mak
nmake -f ms\ntdll.mak install
nmake -f ms\ntdll.mak test

32位debug模式的动态库

perl Configure debug-VC-WIN32 no-asm --prefix = "d:\openssl-101\32debugDll"
ms\do_ms
nmake -f ms\ntdll.mak
nmake -f ms\ntdll.mak install
nmake -f ms\ntdll.mak test

64位release模式的静态库

perl Configure VC-WIN64A no-asm --prefix = ""d:\openssl-101\64releaseLib
ms\do_win64a
nmake -f ms\nt.mak
nmake -f ms\nt.mak install
nmake -f ms\nt.mak test

64位debug模式的静态库

perl Configure debug-VC-WIN64A no-asm --prefix = "d:\openssl-101\64debugLib"
ms\do_win64a
nmake -f ms\nt.mak
nmake -f ms\nt.mak install
nmake -f ms\nt.mak test

64位release模式的动态库

perl Configure VC-WIN64A no-asm --prefix = "d:\openssl-101\64releaseDll"
ms\do_win64a
nmake -f ms\ntdll.mak
nmake -f ms\ntdll.mak install
nmake -f ms\ntdll.mak test

64位debug模式的动态模式

perl Configure debug-VC-WIN64A no-asm --prefix = "d:\openssl-101\64debugDll"
ms\do_win64a
nmake -f ms\ntdll.mak
nmake -f ms\ntdll.mak install
nmake -f ms\ntdll.mak test

1.1.0

1.1.0及以后的版本更简单

perl Configure VC-WIN32 no-asm no-shared

nmake

nmake test

nmake install

即可