I'm working on a C++ application for Windows that uses OpenSSL 1.0.1e library. I'm on Visual Studio 2008.
For portability reasons my application is statically linked against runtime libraries (/MT
and /MTd
options). And I don't ship runtime libs with my application.
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.c
to my project. On my dev machine and on most test computers the program works fine.
But unfortunately I've located computers where the app doesn't start. Windows displays error:
The application failed to initialize properly (0xc0150002). Click on OK to
terminate the application.
I've opened libeay32.dll
in Dependency Walker and I see that MSVCR90.dll
is not found. So the trick with applink.c
doesn't really work for me.
How do I build OpenSSL with /MT
or /MTd
option?
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:
You are expected to install all those tools system-wide and add them to your
%PATH%
environmental variable.After you got everything we need, just follow this simple steps:
Create
C:\build
directory and issue the following command in the command prompt:cd c:\build
Download latest zlib & OpenSSL source codes to your
build
dir by using the following commands:git clone https://github.com/madler/zlib
git clone https://github.com/openssl/openssl
First we have to build static
zlib
. To do that first we will need to edit some configuration files:zlib
source folder:cd C:\build\zlib
Edit the
win32\Makefile.msc
file:CFLAGS
-MD
with-GL -MT -Zc:wchar_t-
LDFLAGS
-debug
with-opt:icf -dynamicbase -nxcompat -ltcg /nodefaultlib:msvcrt
Build
zlib
using 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"
Copy resulting files to your
OpenSSL
directory:xcopy zlib.h C:\build\openssl\
xcopy zconf.h C:\build\openssl\
xcopy zlib.lib C:\build\openssl\
xcopy zlib.pdb C:\build\openssl\
Navigate to
OpenSSL
source:cd C:\build\openssl\
and configure it to use static zlib & read configuration files (openssl.cnf
) fromC:\Windows\
directory.perl Configure VC-WIN64A no-shared zlib no-zlib-dynamic threads --prefix=C:\Windows\
Now make the following edits to the
C:\build\openssl\makefile
:CFLAG
/Zc:wchar_t- /GL /Zi
LDFLAGS
/debug
with/incremental:no /opt:icf /dynamicbase /nxcompat /ltcg /nodefaultlib:msvcrt
EX_LIBS
ZLIB1
withzlib.lib
Build
OpenSSL
by issuing thenmake
command (will take around 15 minutes).The resulting ~3MB
openssl.exe
file will be located atC:\build\openssl\apps\
directory. It is fully portable, since all DLLs are included. If you need to use custom configuration file, copyC:\build\openssl\apps\openssl.cnf
to yourC:\Windows\
directory & edit it to your liking.Use the
nt.mak
makefile rather than thentdll.mak
makefile.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.
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.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.
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.
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.
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
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.
Quoting the instructions:
I tested and it works, in less than 10 min! KUDOS for the authors of the scripts!!
UPDATE: For the x64 builds to be generated you need to install nasm assembler and have it in the PATH.