How to Compile OpenSSL for Visual Studio 2010/2012
We are using OpenSSL for secure communication because… well, anyone with an application communicating over any kind of network should use some kind of secure communication. OpenSSL is free and open source so it’s a good choice for many of the usual reasons.
OpenSSL is multiplatform and requires an on-site build. We are working with Visual Studio 2010 (now moving to 2012) and thus we need to compile it specifically for that environment. I couldn’t find any set of instructions this clear and this precise. This has been cobbled up from many sources and modified by me to fit our scenario. But, I believe this should be useful for anyone compiling OpenSSL for Visual Studio from 2003 and beyond.
Basically, there are two ways to do this: With the Visual Studio assembler or with the Netwide Assembler. I will explain them in tandem. After some (read: many) iterations on this, I’m not sure if there is a problem with either or which one is better. I ended up using the Netwide Assembler. It, however, requires another install. So my suggestion is using the VS assembler and if you have problems you can’t fix, try the Netwide Assembler.
Ingredients:
- Install the Perl compiler for windows. Put it in an easy to reach place like C:\Perl
- Download the latest OpenSSL sources. Put them in an easy to reach place like C:\openssl-<version>
- If you are using the Netwide Assembler (NASM), install that too. Put it in an easy to reach place like C:\NASM
For x86 libraries:
- Open Visual Studio x86 Command Prompt (Should be in your Visual Studio tools subdirectory)
- Change dir to the OpenSSL folder.
- “perl Configure VC-WIN32 no-shared no-idea” (watch the capitals)
- “ms\do_ms.bat” (For NASM it’s “ms\do_nasm.bat”)
- Edit ms\nt.mak file: add “/SAFESEH” to LFLAGS (also, watch the capitals)
- For NASM, Edit ms\nt.mak file: change line 52 to set “C:\NASM\nasm.exe” as the assembly command
- “nmake -f ms\nt.mak”
- This will take a few minutes but in the end you will have two important directories: inc32 and out32. In out32 you will find your two static libraries “libeay32.lib” and “ssleay32.lib”. Put them in a lib directory your linker can use. In inc32 are the headers. Copy all of them into an include directory your compiler can use.
For x64 libraries:
- Open Visual Studio x64 Command Prompt (Should be in your Visual Studio tools subdirectory)
- Change dir to the OpenSSL folder.
- “perl Configure VC-WIN64A no-shared no-idea”
- “ms\do_win64a”
- Edit ms\nt.mak file: add “/SAFESEH” to LFLAGS
- Edit ms\nt.mak file: change directories (lines 37-42) to be 64 and not 32
- For NASM, Edit ms\nt.mak file: change line 52 to set “C:\NASM\nasm.exe” as the assembly command
- “nmake -f ms\nt.mak”
- Same as above only this time the directories are inc64 and out64 (if you changed the directories accordingly in step 6)
Thanks to everyone who helped and/or unknowingly contributed to this guide. If there are problems with it, I happily accept corrections and suggestions.
Posted in Programming by Eran with 4 comments.
Thank you for the useful article. I had to also change line 52 in nt.mak to include /safeseh:
ASM=ml /safeseh /nologo /Cp /coff /c /Cx /Zi
Please extend the article.
Thank you very much!
A huge time saver.
Thanks for the crisp and to the point article. However how do I build for MTd, MD and MDd options.
I’m sorry but I don’t remember.