C语言 如何构建与 Windows 运行时静态链接的 OpenSSL?

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

How do I build OpenSSL statically linked against Windows runtime?

cwindowsopensslstatic-librariesstatic-linking

提问by user2724991

I'm working on a C++ application for Windows that uses OpenSSL 1.0.1e library. I'm on Visual Studio 2008.

我正在为使用 OpenSSL 1.0.1e 库的 Windows 开发 C++ 应用程序。我在 Visual Studio 2008 上。

For portability reasons my application is statically linked against runtime libraries (/MTand /MTdoptions). And I don't ship runtime libs with my application.

出于可移植性的原因,我的应用程序与运行时库(/MT/MTd选项)静态链接。而且我不随我的应用程序提供运行时库。

Per the OpenSSL FAQ, the library is by default linked against multithreaded DLL runtime (/MDd) which is obviously incompatible with my scenario. So to make my program work I've added applink.cto my project. On my dev machine and on most test computers the program works fine.

根据OpenSSL FAQ,该库默认链接到多线程 DLL 运行时 ( /MDd),这显然与我的场景不兼容。因此,为了使我的程序正常工作,我已将其添加applink.c到我的项目中。在我的开发机器和大多数测试计算机上,程序运行良好。

But unfortunately I've located computers where the app doesn't start. Windows displays error:

但不幸的是,我找到了无法启动应用程序的计算机。Windows 显示错误:

The application failed to initialize properly (0xc0150002). Click on OK to
terminate the application. 

I've opened libeay32.dllin Dependency Walker and I see that MSVCR90.dllis not found. So the trick with applink.cdoesn't really work for me.

我已经libeay32.dll在 Dependency Walker 中打开了,我MSVCR90.dll发现没有找到。所以这个技巧applink.c对我来说真的不起作用。

How do I build OpenSSL with /MTor /MTdoption?

如何使用/MT/MTd选项构建 OpenSSL ?

回答by Len Holgate

Use the nt.makmakefile rather than the ntdll.makmakefile.

使用nt.makmakefile 而不是ntdll.makmakefile。

As an aside, I have written some scripts around the standard OpenSSL build scripts which make it 'easier' (for me at least) to use OpenSSL on Windows with a mix of both x86 and x64, you can get them from here.

顺便说一句,我已经围绕标准 OpenSSL 构建脚本编写了一些脚本,这使得在混合 x86 和 x64 的 Windows 上使用 OpenSSL 变得“更容易”(至少对我而言),您可以从这里获取它们。

回答by Fernando Gonzalez Sanchez

The most elegant option I have found for Windows involves using the scripts provided at http://p-nand-q.com/programming/windows/building_openssl_with_visual_studio_2013.html

我为 Windows 找到的最优雅的选择是使用http://p-nand-q.com/programming/windows/building_openssl_with_visual_studio_2013.html提供的脚本

They provide scripts for VS2010/VS2013/VS2015 for each script version it builds all combinations of x86/x86-64 with runtimes MDd/MD/MTd/MT.

他们为每个脚本版本提供 VS2010/VS2013/VS2015 的脚本,它构建了 x86/x86-64 与运行时 MDd/MD/MTd/MT 的所有组合。

Quoting the instructions:

引用说明:

PREREQUISITES:

The script assumes you are on Windows.

The script assumes you have Visual Studio 2010, 2013 or 2015 installed in all the usual places. Important: If you have a different installation folder, your mileage may vary

The script assumes you have downloaded an OpenSSL tarball, like this one.

The script assumes you have Python (2.7 or 3.x) installed and on your PATH

The script assumes you have 7-zip installed (doesn't need to be on your PATH) Choose the script you want to use and edit it. For example, let's take a look at the top of rebuild_openssl_vs2015.cmd:

T:

set OPENSSL_VERSION=1.0.1p

set SEVENZIP="C:\Program Files\7-Zip\7z.exe"

set VS2015="C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\vcvars32.bat"

set VS2015_AMD64="C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64\vcvars64.bat"

so it is pretty easy to see: you must enter the OpenSSL version manually, the rest should have sensible defaults...

Note: The script uses the SUBST T:\ drive for building OpenSSL.

先决条件:

该脚本假定您使用的是 Windows。

该脚本假定您已在所有常用位置安装了 Visual Studio 2010、2013 或 2015。重要提示:如果您有不同的安装文件夹,您的里程可能会有所不同

该脚本假定您已经下载了一个 OpenSSL tarball,就像这个。

该脚本假定您已安装 Python(2.7 或 3.x)并在您的 PATH 上

该脚本假定您已安装 7-zip(不需要在您的 PATH 上)选择您要使用的脚本并对其进行编辑。比如我们看一下rebuild_openssl_vs2015.cmd的顶部:

:

设置 OPENSSL_VERSION=1.0.1p

设置 SEVENZIP="C:\Program Files\7-Zip\7z.exe"

设置 VS2015="C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\vcvars32.bat"

设置 VS2015_AMD64="C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64\vcvars64.bat"

所以很容易看出:你必须手动输入 OpenSSL 版本,其余的应该有合理的默认值......

注意:该脚本使用 SUBST T:\ 驱动器来构建 OpenSSL。

I tested and it works, in less than 10 min! KUDOS for the authors of the scripts!!

我测试了它,它在不到 10 分钟的时间内就起作用了!为脚本作者点赞!!

UPDATE: For the x64 builds to be generated you need to install nasm assembler and have it in the PATH.

更新:要生成 x64 版本,您需要安装 nasm 汇编程序并将其放在 PATH 中。

回答by Anubioz

To build 64-bit OpenSSL statically linked (which results in a single .exe file without any DLLs) with Visual Studio 2015, you will need the following prerequisites:

要使用 Visual Studio 2015 构建 64 位 OpenSSL 静态链接(这会生成一个没有任何 DLL 的单个 .exe 文件),您需要满足以下先决条件:

You are expected to install all those tools system-wide and add them to your %PATH%environmental variable.

您应该在系统范围内安装所有这些工具并将它们添加到您的%PATH%环境变量中。

After you got everything we need, just follow this simple steps:

获得我们需要的一切后,只需按照以下简单步骤操作:

  1. Open VS2015 x64 Native Tools Command Promptfrom your Start Menu. You will see command prompt.
  2. Create C:\builddirectory and issue the following command in the command prompt:

    • cd c:\build
  3. Download latest zlib & OpenSSL source codes to your builddir by using the following commands:

    • git clone https://github.com/madler/zlib
    • git clone https://github.com/openssl/openssl
  4. First we have to build static zlib. To do that first we will need to edit some configuration files:

    • Navigate to the zlibsource folder: cd C:\build\zlib
    • Edit the win32\Makefile.mscfile:

      1. Find the line starting with CFLAGS
      2. Replace -MDwith -GL -MT -Zc:wchar_t-
      3. Find the line starting with LDFLAGS
      4. Replace -debugwith -opt:icf -dynamicbase -nxcompat -ltcg /nodefaultlib:msvcrt
  5. Build zlibusing the following command (should take less than a minute):

    • nmake -f win32/Makefile.msc AS=ml64 LOC="-DASMV -DASMINF -DNDEBUG -I." OBJA="inffasx64.obj gvmat64.obj inffas8664.obj"
  6. Copy resulting files to your OpenSSLdirectory:

    • xcopy zlib.h C:\build\openssl\
    • xcopy zconf.h C:\build\openssl\
    • xcopy zlib.lib C:\build\openssl\
    • xcopy zlib.pdb C:\build\openssl\
  7. Navigate to OpenSSLsource: cd C:\build\openssl\and configure it to use static zlib & read configuration files (openssl.cnf) from C:\Windows\directory.

    • perl Configure VC-WIN64A no-shared zlib no-zlib-dynamic threads --prefix=C:\Windows\
  8. Now make the following edits to the C:\build\openssl\makefile:

    • Find the line that starts with: CFLAG
    • Append: /Zc:wchar_t- /GL /Zi
    • Find the line that starts with: LDFLAGS
    • Replace /debugwith /incremental:no /opt:icf /dynamicbase /nxcompat /ltcg /nodefaultlib:msvcrt
    • Find the line that starts with: EX_LIBS
    • Replace ZLIB1with zlib.lib
    • Save changes
  9. Build OpenSSLby issuing the nmakecommand (will take around 15 minutes).

  1. 从开始菜单打开VS2015 x64 Native Tools 命令提示符。您将看到命令提示符。
  2. 创建C:\build目录并在命令提示符下发出以下命令:

    • cd c:\build
  3. build使用以下命令将最新的 zlib 和 OpenSSL 源代码下载到您的目录:

    • git clone https://github.com/madler/zlib
    • git clone https://github.com/openssl/openssl
  4. 首先,我们必须构建 static zlib。首先,我们需要编辑一些配置文件:

    • 导航到zlib源文件夹:cd C:\build\zlib
    • 编辑win32\Makefile.msc文件:

      1. 找到以开头的行 CFLAGS
      2. 替换-MD-GL -MT -Zc:wchar_t-
      3. 找到以开头的行 LDFLAGS
      4. 替换-debug-opt:icf -dynamicbase -nxcompat -ltcg /nodefaultlib:msvcrt
  5. zlib使用以下命令构建(应该不到一分钟):

    • nmake -f win32/Makefile.msc AS=ml64 LOC="-DASMV -DASMINF -DNDEBUG -I." OBJA="inffasx64.obj gvmat64.obj inffas8664.obj"
  6. 将生成的文件复制到您的OpenSSL目录:

    • xcopy zlib.h C:\build\openssl\
    • xcopy zconf.h C:\build\openssl\
    • xcopy zlib.lib C:\build\openssl\
    • xcopy zlib.pdb C:\build\openssl\
  7. 导航到OpenSSLsource:cd C:\build\openssl\并将其配置为使用静态 zlib 并openssl.cnfC:\Windows\目录中读取配置文件 ( ) 。

    • perl Configure VC-WIN64A no-shared zlib no-zlib-dynamic threads --prefix=C:\Windows\
  8. 现在对 进行以下编辑C:\build\openssl\makefile

    • 找到以以下内容开头的行: CFLAG
    • 附加: /Zc:wchar_t- /GL /Zi
    • 找到以以下内容开头的行: LDFLAGS
    • 替换/debug/incremental:no /opt:icf /dynamicbase /nxcompat /ltcg /nodefaultlib:msvcrt
    • 找到以以下内容开头的行: EX_LIBS
    • 替换ZLIB1zlib.lib
    • 保存更改
  9. OpenSSL通过发出nmake命令来构建(大约需要 15 分钟)。

The resulting ~3MB openssl.exefile will be located at C:\build\openssl\apps\directory. It is fully portable, since all DLLs are included. If you need to use custom configuration file, copy C:\build\openssl\apps\openssl.cnfto your C:\Windows\directory & edit it to your liking.

生成的 ~3MBopenssl.exe文件将位于C:\build\openssl\apps\目录中。它是完全可移植的,因为包含所有 DLL。如果您需要使用自定义配置文件,请复制C:\build\openssl\apps\openssl.cnf到您的C:\Windows\目录并根据您的喜好进行编辑。

回答by sigma

If you want precompiled OpenSSL libraries with MT look here: http://www.npcglib.org/~stathis/blog/precompiled-openssl/You will find a patch for the OpenSSL sources that enables producing libraries with suffixes MT/MD and "d" for debug to make identifying the libraries easier.

如果您想要使用 MT 的预编译 OpenSSL 库,请查看此处:http: //www.npcglib.org/~stathis/blog/precompiled-openssl/您将找到 OpenSSL 源代码的补丁,可以生成带有后缀 MT/MD 和“的库” d" 进行调试,以便更轻松地识别库。

What's more, you will also find the actual build script to build all of them at once for many different version of Visual Studio. I build and use them myself to exactly produce binaries that need no DLLs for my projects and you may find them useful.

更重要的是,您还将找到实际的构建脚本,以便为许多不同版本的 Visual Studio 一次构建所有这些脚本。我自己构建并使用它们来准确地为我的项目生成不需要 DLL 的二进制文件,您可能会发现它们很有用。

回答by computerquip

It appears OpenSSL now links with -MT -Zl(at least when using msvc) meaning it discards default named libraries which are then decided in your final binary. Applications appear to use the static runtime by default.

看来 OpenSSL 现在与-MT -Zl(至少在使用 msvc 时)链接,这意味着它会丢弃默认命名库,然后在您的最终二进制文件中决定这些库。默认情况下,应用程序似乎使用静态运行时。

In other words, no action needs to be taken in order to use it with your binary, just provide whatever flag you want and the OpenSSL library will just work with it. Unfortunate there isn't a lot of concrete documentation on building such an important library.

换句话说,无需采取任何措施即可将它与您的二进制文件一起使用,只需提供您想要的任何标志,OpenSSL 库就可以使用它。不幸的是,关于构建如此重要的库的具体文档并不多。