7 December, 2015 (published)
  7 December, 2015 (last modified)

Compiling OpenSSL on Windows (mingw32)

  Categories: c++, MS Windows, Qt
  Tags:  , , ,

mingwThe present post is a recipe for building static and dynamic OpenSSL libraries for all versions of OpenSSL on the Windows platform, viz. mingw using the gnu c++ compiler. In another posts we treat the msvc (Microsoft Visual Studio C++ compiler) toolchain. Any mingw toolchain could be used for the present purpose. However, there are quite a number of mingw distributions around that differ in a not well-documented way. As in our company we build multi-platform (Windows, Mac and Linux) applications using the Qt IDE we will use the mingw distribution (version 492-32bit) that comes with the open source Qt package (version 5.5.1 at present). Our goal is to have in the end the following 2 static libraries:

libcrypto.a
libssl.a

The following 2 import libraries

libcrypto.dll.a
libssl.dll.a

And the following 2 dynamic libraries:

libeay32.dll 
ssleay32.dll

Quick editing of the PATH environment variable

You will have to run several applications from the command line to compile OpenSSL. For your convenience it is advisable to have the location of these applications in your PATH environment. In earlier days one had to restart Windows to have a change in the SYSTEM environment to become active. This awkward situation has changed now. We use the free gui utility Rapid Environment Editor with which you can edit the SYSTEM and USER environment variables in such a way that these changes become immediately active if you open a *new* instance of explorer or a *new* instance of the MS-DOS terminal window (cmd.exe). Sometimes Windows caches the environment variables in such a way that a new change will not become active. This unwanted situation could occur for instance after installing a new program that adds its own file location to the PATH environment. To force to refresh the cache just edit the PATH variable and save and then undo the previous edit and save again. Now the most actual PATH will be known to all applications that need the environment for their execution.

Requirements

All the following requirements are essential. Do not go to a next requirement if you have not fulfilled the previous requirement.

  1. perl
    You will need the perl program. Open an MS-DOS command window and run

    perl -v

    If you get something like:

    'perl' is not recognized as an internal or external command ...

    you do not have perl installed. We advise to use the free ActiveState perl distribution. Then running the previous command should give something like:

    This is perl 5, version 20, subversion 2 (v5.20.2) built for MSWin32-x64-multi-thread  (with 1 registered patch, see perl -V for more detail) Copyright 1987-2015, Larry Wall Binary build 2002 [299195] provided by ActiveState http://www.ActiveState.com

    You are OK now.

  2. Mingw32
    I assume you have somewhere a mingw distribution whose gcc compiler you want to use. Open an MS-DOS cmd window at the file location of this gcc compiler of the mingw32 distribution and run

    gcc -v

    The last line of the response should be something like

    gcc version 4.8.0 (rev2, Built by MinGW-builds project) ..

    Open another MS-DOS window at root c:\ and run again

    gcc -v

    If the response is exactly the same as you had before you have the right mingw toolchain and it is also in your PATH. If you get another response you have to add the mingw location to your PATH and/or you have to remove from your PATH the location containing other gcc compilers.

  3. GNU make
    You will need the GNU make utility and it must be in your PATH. On a Windows development computer there are usually a number of programs called "make", and worse some of them are in your PATH. Open a MS-DOS window at root location and run

    make -v

    The response should be something like

    GNU Make 3.81 ...

    Any other response, like a Microsoft "make" is wrong. Get these other "make"'s out of the PATH (possibly only temporarily). If you still do not get the GNU response you will have to install GNU make. Download it from the internet (it is shipped with MSYS).

  4. tar
    The OpenSSL source comes as a compressed (gz) archive (tar) which in the Linux world is called a tarball. The OpenSSL archive (may) contain file links. Although Windows supports some form of file links their implementation is not compatible with Linux-type links. As a result the popular zip/unzip Windows utilities can produce incomplete uncompressed archives if there  are file links in the archive. For this purpose we strongly advise  to use the GNU "tar" utility to untar the tarball. The tar utility ships with the above mentioned MSYS distribution. So open an MS-DOS terminal at root and run

    tar --version

    The response should be something like:

    tar (GNU tar) 1.23Copyright (C) 2010 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.Written by John Gilmore and Jay Fenlason.

Dynamic link libraries

We will now compile for the case of generating dynamic link libraries.

  1. Download the required OpenSSL version. It is a compressed file (tarball tar.gz. ) Untar the tarball with tar. Open a MS-DOS terminal window at the location of the tarball and run
    tar -xf vv.tar.gz

    where vv is the OpenSSL version (like 1.0.2e). Neglect the warnings about file links. Check that the tarball is indeed decompressed in its own new directory.

  2. Open an MS-DOS cmd window at the root location of the OpenSSL source distribution. That directory should have a file called "Configure". Run
    perl Configure mingw shared

    it should do some stuff ( a minute or so) and should end with something like

    Configured for mingw

  3. Run in the same terminal
    make

    The terminal window should start compiling and after some time (10 minutes in our case) it should end with no errors.

  4. The two dll's and the two import libraries mentioned in the beginning of this post can be found at the root of the source directory. You can move them to a suitable location. You probably also need the include/openssl directory. Please check that the files in this directory are real files and not file links. If they are file links copy them using a MS-DOS window with "copy", as this will automatically replace the links with the real files.

Static libraries

If you want to generate static libraries the procedure is almost identical to the one described for the case of dynamic libraries. Just replace

perl Configure mingw shared

with

perl Configure mingw no-shared

You now can collect the two static libraries and the include branch.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Write a reaction

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution.