C++ 如何使用 Visual Studio 2017 在 Windows 上构建 OpenSSL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45494630/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:39:06  来源:igfitidea点击:

How to build OpenSSL on Windows with Visual Studio 2017?

c++visual-studiobuildopenssl

提问by AeroSun

I am trying to use OpenSSL but I am stuck on the step of compiling. The OpenSSL project has very unfriendly (bad) documentation.

我正在尝试使用 OpenSSL,但我被困在编译步骤中。OpenSSL 项目有非常不友好(坏)的文档。

Is there any actual help how to build the latest OpenSSL version on Windows with Visual Studio 2017?

如何使用 Visual Studio 2017 在 Windows 上构建最新的 OpenSSL 版本是否有任何实际帮助?

I didn't find any helpful information on the official OpenSSL site. Yes, there are a lot of posts on the Internet about OpenSSL compilation, but all of them are obsolete.

我在 OpenSSL 官方网站上没有找到任何有用的信息。是的,网上有很多关于 OpenSSL 编译的帖子,但都已经过时了。

回答by Matt Caswell

I've not used VS2017 but previous versions. I imagine it is much the same. In brief the steps are:

我没有使用过 VS2017,而是以前的版本。我想它是一样的。简而言之,步骤是:

  1. Install Perl (either ActiveState or Strawberry)

  2. Install NASM

  3. Make sure both Perl and NASM are on your %PATH%

  4. Fire up a Visual Studio Developer Command Prompt with administrative privileges (make sure you use the 32-bit one if you are building 32-bit OpenSSL, or the 64-bit one if you are building 64-bit OpenSSL)

  5. From the root of the OpenSSL source directory enter perl Configure VC-WIN32, if you want 32-bit OpenSSL or perl Configure VC-WIN64Aif you want 64-bit OpenSSL

  6. Enter nmake

  7. Enter nmake test

  8. Enter nmake install

  1. 安装 Perl(ActiveState 或 Strawberry)

  2. 安装 NASM

  3. 确保 Perl 和 NASM 都在你的 %PATH% 上

  4. 使用管理权限启动 Visual Studio 开发人员命令提示符(如果您正在构建 32 位 OpenSSL,请确保使用 32 位,或者如果您正在构建 64 位 OpenSSL,请确保使用 64 位)

  5. 从 OpenSSL 源目录的根目录输入perl Configure VC-WIN32,如果您想要 32 位 OpenSSL 或perl Configure VC-WIN64A如果您想要 64 位 OpenSSL

  6. 进入 nmake

  7. 进入 nmake test

  8. 进入 nmake install

If anything goes wrong at any stage, check the INSTALLfile and the NOTES.WINfile.

如果任何阶段出现任何问题,请检查INSTALL文件和NOTES.WIN文件。

回答by The Quantum Physicist

For OpenSSL 1.0.2, I wrote a Python script that does the building for me. I have this habit of making these scripts, as I don't like to reinvent the wheel everytime I need to build something.

对于 OpenSSL 1.0.2,我编写了一个 Python 脚本来为我进行构建。我有制作这些脚本的习惯,因为我不喜欢每次需要构建一些东西时都重新发明轮子。

The script is made for OpenSSL 1.0.2. Probably the changes are minimal for OpenSSL 1.1.0.

该脚本是为 OpenSSL 1.0.2 制作的。OpenSSL 1.1.0 的变化可能很小。

Here's the script:

这是脚本:

import os
from subprocess import call
import sys
import re

vs_version = "140"
compile_flags = "-no-asm -no-shared"


openssl_32_flag = "VC-WIN32"
openssl_64_flag = "VC-WIN64A"

src_32_suffix = "_" + "vs" + vs_version + "_32"
src_64_suffix = "_" + "vs" + vs_version + "_64"

vs_tools_env_var = "VS" + vs_version + "COMNTOOLS"

if len(sys.argv) < 2:
    print("First argument must be the tar.gz file of OpenSSL source")
    exit(1)

if len(sys.argv) < 3:
    print("Second argument must be 32 or 64")
    exit(1)

filename = sys.argv[1]
dirname  = filename.replace(".tar.gz","")
working_dir = os.getcwd()
arch = sys.argv[2]

if arch != "32" and arch != "64":
    print("Second argument must be 32 or 64")
    exit(1)


if not bool(re.match("(openssl-){1}(\d)+(.)(\d)+(.)(\d)+(\w)+(.tar.gz)",filename)):
    print("The file given doesn't seem to be an openssl source file. It must be in the form: openssl-x.y.zw.tar.gz")
    exit(1)


call("7z x " + filename) #extract the .gz file

dirname_src_32 = dirname + src_32_suffix
dirname_src_64 = dirname + src_64_suffix
dirname_bin_32 = dirname + src_32_suffix + "_build"
dirname_bin_64 = dirname + src_64_suffix + "_build"

openssl_tar_file = filename[0:-3]

if arch == "32":
#extract tar file for 32
    call("7z x " + openssl_tar_file)
    os.rename(dirname, dirname_src_32)

#Compile 32
    os.chdir(dirname_src_32)

    call("perl Configure " + openssl_32_flag + " --prefix=" + os.path.join(working_dir,dirname_bin_32) + " " + compile_flags,shell=True)
    call(r"ms\do_ms.bat",shell=True)
    call(r"nmake -f ms\nt.mak",shell=True)
    call(r"nmake -f ms\nt.mak instalL",shell=True)

    print("32-bit compilation complete.")

#Go back to base dir
os.chdir(working_dir)
################

if arch == "64":
#extract for 64
    call("7z x " + openssl_tar_file)
    os.rename(dirname, dirname_src_64)

#Compile 64
    os.chdir(dirname_src_64)

    call("perl Configure " + openssl_64_flag + " --prefix=" + os.path.join(working_dir,dirname_bin_64) + " " + compile_flags,shell=True)
    call(r"ms\do_ms.bat",shell=True)
    call(r"nmake -f ms\nt.mak",shell=True)
    call(r"nmake -f ms\nt.mak instalL",shell=True)

    print("64-bit compilation complete.")

#Go back to base dir
os.chdir(working_dir)
################

os.remove(openssl_tar_file)

Option 1: Save the script to CompileOpenSSL.py, and download the OpenSSL source file that is expected to have the name format openssl-1.X.Y.tar.gz. Now assuming that 7zip and perl are accessible from the global scope on your command prompt and you have the correct MSVC variables loaded (with e.g. vsvars32.bat, or starting the right terminal), run the following:

选项 1:将脚本保存到CompileOpenSSL.py,并下载预期具有名称格式的 OpenSSL 源文件openssl-1.X.Y.tar.gz。现在假设 7zip 和 perl 可以从命令提示符的全局范围访问,并且您加载了正确的 MSVC 变量(例如vsvars32.bat,或启动正确的终端),运行以下命令:

python CompileOpenSSL.py openssl-1.X.Y.tar.gz 32

If you're using MSVC 32-bit, or

如果您使用的是 MSVC 32 位,或者

python CompileOpenSSL.py openssl-1.X.Y.tar.gz 64

for MSVC 64-bit.

对于 MSVC 64 位。

Option 2: Do what the script does manually. The script simply extracts the archive, configures the sources and runs do_ms.batthen nmake. Follow the source and it'll work.

选项 2:执行脚本手动执行的操作。该脚本只是提取存档,配置的来源和运行do_ms.bat,然后nmake。按照来源,它会工作。

Good luck!

祝你好运!

回答by TTCOIN DEVELOPERS ttcoinfounda

  1. go into ssl directory using visual studio cmd and add perl and nasm to system path then type:

  2. perl Configure --openssldir=D:OpenSSLdirectory VC-WIN32

  3. ms\do_ms.bat

  4. nmake -f ms\ntdll.mak

  5. nmake -f ms\ntdll.mak install

  6. ( enjoy. )

  1. 使用 Visual Studio cmd 进入 ssl 目录并将 perl 和 nasm 添加到系统路径,然后键入:

  2. perl Configure --openssldir=D:OpenSSLdirectory VC-WIN32

  3. ms\do_ms.bat

  4. nmake -f ms\ntdll.mak

  5. nmake -f ms\ntdll.mak install

  6. ( 请享用。 )

回答by Ti-R

Modified version of The Quantum Physicist python script

The Quantum Physicist python 脚本的修改版本

It can compile OpenSSL 1.0.x or OpenSSL 1.1.x

它可以编译 OpenSSL 1.0.x 或 OpenSSL 1.1.x

It can compile with multiple version of Visual Studio 2017/2019 included.

它可以编译包含多个版本的 Visual Studio 2017/2019。

1) Create the file: CompileOpenSSL.py

1) 创建文件:CompileOpenSSL.py

import os
import os.path
from subprocess import call
import shutil
import sys
import re
import argparse

# args
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--filename", help="First argument must be the tar.gz file of OpenSSL source", required=True)
parser.add_argument("-a", "--arch", help="Second argument must be x86 or amd64", required=True)
parser.add_argument("-v", "--vs_version", help="Visual Studio version (eg:90, 140, 150)", required=True)
parser.set_defaults(writeVersionInfos=False)
args = parser.parse_args()

compile_flags = "-no-asm"
#compile_flags = "-no-asm -no-shared"

openssl_32_flag = "VC-WIN32"
openssl_64_flag = "VC-WIN64A"

working_dir = os.getcwd()

dirname  = args.filename.replace(".tar.gz","")

src_32_suffix = "_" + "vs" + args.vs_version + "_32"
src_64_suffix = "_" + "vs" + args.vs_version + "_64"

vs_tools_env_var = "VS" + args.vs_version + "COMNTOOLS"


if args.arch != "x86" and args.arch != "amd64":
    print("Second argument must be x86 or amd64")
    exit(1)


if not bool(re.match("(openssl-){1}(\d)+(.)(\d)+(.)(\d)+(\w)+(.tar.gz)",args.filename)):
    print("The file given doesn't seem to be an openssl source file. It must be in the form: openssl-x.y.zw.tar.gz")
    exit(1)


call("7z x -y " + args.filename) #extract the .gz file

dirname_src_32 = dirname + src_32_suffix
dirname_src_64 = dirname + src_64_suffix
dirname_bin_32 = dirname + src_32_suffix + "_build"
dirname_bin_64 = dirname + src_64_suffix + "_build"

openssl_tar_file = args.filename[0:-3]

if args.arch == "x86":

#delete previous directories
    shutil.rmtree(os.getcwd()+'/'+dirname, ignore_errors=True)
    shutil.rmtree(os.getcwd()+'/'+dirname_src_32, ignore_errors=True)

#extract tar file for 32

    call("7z x -y " + openssl_tar_file)
    os.rename(dirname, dirname_src_32)

#Compile 32
    os.chdir(dirname_src_32)

    print("perl Configure " + openssl_32_flag + " --prefix=" + os.path.join(working_dir,dirname_bin_32) + " " + compile_flags)
    call("perl Configure " + openssl_32_flag + " --prefix=" + os.path.join(working_dir,dirname_bin_32) + " " + compile_flags,shell=True)

    if( os.path.exists("ms/do_ms.bat") ):
        call("ms\do_ms.bat",shell=True)
        print(os.getcwd())
        call("nmake -f ms/ntdll.mak",shell=True)
        call("nmake -f ms/ntdll.mak install",shell=True)
    else:
        call("nmake",shell=True)
        call("nmake test",shell=True)
        call("nmake install",shell=True)

    print("32-bit compilation complete.")

#Go back to base dir
os.chdir(working_dir)
################

if args.arch == "amd64":

#delete previous directories
    shutil.rmtree(os.getcwd()+'/'+dirname, ignore_errors=True)
    shutil.rmtree(os.getcwd()+'/'+dirname_src_64, ignore_errors=True)


#extract for 64
    call("7z x -y " + openssl_tar_file)
    os.rename(dirname, dirname_src_64)

#Compile 64
    os.chdir(dirname_src_64)

    call("perl Configure " + openssl_64_flag + " --prefix=" + os.path.join(working_dir,dirname_bin_64) + " " + compile_flags,shell=True)
    if( os.path.exists("ms\do_ms.bat") ):
        call("ms\do_win64a.bat",shell=True)
        call("nmake -f ms/ntdll.mak",shell=True)
        call("nmake -f ms/ntdll.mak install",shell=True)
    else:
        call("nmake",shell=True)
        call("nmake test",shell=True)
        call("nmake install",shell=True)

    print("64-bit compilation complete.")

#Go back to base dir
os.chdir(working_dir)
################

os.remove(openssl_tar_file)

2) Create the file: CompileOpenSSL_vs.cmd

2)创建文件:CompileOpenSSL_vs.cmd

ECHO  --------------------------------------
ECHO Require Python, 7Zip, PERL and NASM in PATH
ECHO  --------------------------------------

Rem ------------------------------------------------------
Rem TO CONFIGURE -----------------------------------------
Rem ------------------------------------------------------

Rem SET YOUR LOCAL PATHS-----------------------------------------
SET PATH=C:\Program Files (x86)-Zip;C:\Perl64\bin;M:\Backup\Coders\_tools-Zip\;%PATH% 

Rem SET YOUR OPENSSL ARCHIVE-----------------------------------------
REM SET FILENAME=openssl-1.0.2r.tar.gz 
SET FILENAME=openssl-1.1.1b.tar.gz

Rem SET THE VERSION OF YOUR VISUAL STUDIO-----------------------------------------
SET VSVERSION=%1


Rem ------------------------------------------------------
Rem COMPILATION LAUNCH -----------------------------------
Rem ------------------------------------------------------

Rem UTILS PATH-----
SET VSCOMNTOOLSNAME=VS%VSVERSION%COMNTOOLS

Rem Pick the good path for Visual Studio-----------------------------------------
IF %VSVERSION% GEQ 150 (
    Echo DO NOT FORGET TO ADD A SYSTEM VARIABLE %VSCOMNTOOLSNAME% - like: "C:\Program Files (x86)\Microsoft Visual Studio17\Community\Common7\Tools\"
    SET VCVARPATH="%%%VSCOMNTOOLSNAME%%%..\..\VC\Auxiliary\Build\vcvarsall.bat"
) ELSE (
    SET VCVARPATH="%%%VSCOMNTOOLSNAME%%%..\..\VC\vcvarsall.bat"
)

Rem Set env -----------------------------------------
@pushd "%~dp0"
call %VCVARPATH% %2
@popd

Rem ------------------------------------------------------
Rem TEST APP EXIST -----------------------------------
Rem ------------------------------------------------------

where /q 7z.exe
IF ERRORLEVEL 1 (
    ECHO The application "7z.exe" is missing. Ensure to add/install it to the PATH in beginning of this script, check SET PATH
    PAUSE
    EXIT /B
)

where /q perl.exe
IF ERRORLEVEL 1 (
    ECHO The application "perl.exe" is missing. Ensure to add/install it to the PATH in beginning of this script, check SET PATH
    PAUSE
    EXIT /B
)

where /q nmake.exe
IF ERRORLEVEL 1 (
    ECHO The application "nmake.exe" is missing. Ensure to add/install it to the PATH in beginning of this script, check SET PATH
    PAUSE
    EXIT /B
)

where /q py.exe
IF ERRORLEVEL 1 (
    ECHO The application "py.exe" [shortcut of python] is missing. Ensure to add/install it to the PATH in beginning of this script, check SET PATH
    PAUSE
    EXIT /B
)

Rem Launch compilation -----------------------------------------

py CompileOpenSSL.py -f %FILENAME% -a %2 -v %VSVERSION%


PAUSE

3) Launch compilation from command line (Outside Visual Studio) eg:

3)从命令行(Visual Studio 外部)启动编译,例如:

CompileOpenSSL_vs.cmd 150 x86
CompileOpenSSL_vs.cmd 150 amd64

CompileOpenSSL_vs.cmd 90 x86