Compiling static OpenSSL with /MD[d]
So, you have a DLL that uses OpenSSL. Maybe your DLL uses libcurl and you want to support HTTPS? And, you want to statically link to avoid distributing multiple files.
You’re couldn’t find a static build of the OpenSSL libs, could you? And when you downloaded the source, you found that only supports static linkage as an afterthought, and when it does, it links with /MT.
Static MSVCRT. How quaint.
DLLs pretty much always compile with /MD (and /MDd in debug mode). And, in fact, it’s not just DLLs; statically linking with /MT is just simply a bad idea these days, because you need to update the C runtime libraries often. Your DLL needs static .lib versions of OpenSSL compiled with /MD (/MDd for debugging). Those aren’t provided. And the makefiles won’t build them. And there are no instructions on how to build them.
Let’s be honest, statically linking against OpenSSL is not a great idea either. But there are good business reasons for what you’re doing, and I’m not going to judge. I’m writing this because I’m doing the same thing.
32-bit build
Here’s my build process for a 32-bit debug build, based on openssl-1.0.2h. Open a Visual Studio command prompt for your target version (I’m using 2010), then run these commands:
perl Configure no-asm debug-VC-WIN32 --prefix=f:\openssl-32-debug ms\do_ms !!! HACK ms t.mak to replace /MTd with /MDd: I found only one !!! nmake -f ms t.mak nmake -f ms t.mak install
You can repeat this for the non-debug target with /MD, and repeat the whole process in a different directory tree for the 64 bit builds, as shown in other people’s examples.
Notes:
- I couldn’t get the assembler to compile even with nasm installed, so I just gave up on that (hence “no-asm”).
- Version 1.0.2a gave me link errors even with a vanilla build, so I think it’s broken.
64-bit build
It’s almost the same. In the 64-bit Visual Studio command prompt:
perl Configure no-asm debug-VC-WIN32 --prefix=f:\openssl-64-debug ms\do_win64a !!! HACK ms t.mak to replace /MTd with /MDd as with the 32-bit build !!! nmake -f ms t.mak nmake -f ms t.mak install
Bonus Credit
For bonus points, also run the tests.
nmake -f ms t.mak test
Extra Bonus: Assembly Code
Daboul at stackoverflow got the assembly to compile. According to his comment on this answer, this script will do it:
call cd src\openssl-1.0.2k call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat" call set path=C:\Perl64\bin;C:\Users\m\AppData\Local\NASM;%path% call perl Configure VC-WIN32 --prefix="../../build/1.0.2k-x86_rel_MD" call ms\do_nasm.bat powershell -Command "(Get-Content ms t.mak).replace('/MT', '/MD') | Set-Content ms t.mak" call nmake -f ms t.mak call nmake -f ms t.mak install