How to include OpenSSL in Visual Studio

Solution 1:

Intro

  • Although the question / answer is based on:

    • OpenSSL 1.0.1*
    • VStudio 2012


    it applies to any OpenSSL version (v.0.9.*, v1.0.*, v1.1.*, v3.0), and any VStudio version (v2005+)

  • OpenSSL builds are also available at [GitHub]: CristiFati/Prebuilt-Binaries - (master) Prebuilt-Binaries/OpenSSL



Let's assume you have installed OpenSSL in a dir like: "C:\Program Files\Openssl-Win32-1.0.1p....." (or whatever other name); I am going to refer to that as OPENSSL_INSTALL_DIR (as it was an Nix style env var). So, ${OPENSSL_INSTALL_DIR} should contain a bunch of dirs and files, out of which matter for us:

  • Dirs:

    • include
    • lib
  • Files (since their names vary across versions, I'm going to refer to them using (again, Nix style var) placeholders; also they might be located in a bin subdir):

    • ${LIBCRYPTO}.dll
    • ${LIBSSL}.dll


    where LIBCRYPTO (#1.) and LIBSSL (#2.) are defined as follows:

    • v1.0.2 and older

      1. libeay32
      2. ssleay32 (might be also copied (or symlinked) to libssl32)
    • v1.1.*

      1. libcrypto-1_*(-x64)
      2. libssl-1_*(-x64)
    • v3.0

      1. libcrypto-3*
      2. libssl-3*

In order to make use of it, in your VStudio project you have to:

1. Configue the compiler ([MS.Docs]: Compiler Options Listed Alphabetically)
Instruct it:

  • Where to search for include (header (.h)) files. Go to your "Project Properties -> C/C++ -> General -> Additional Include Directories" and adding ${OPENSSL_INSTALL_DIR}\include (if you need to add other paths, separate them by a semicolon (;)). Now you can include in your source code OpenSSL header files.
    Note that because "${OPENSSL_INSTALL_DIR}\include" dir contains an openssl subdir and under that subdir are the actual header files, your #include clauses would have to look like this:

    #include <openssl/ssl.h>
    

    Of course you could add ${OPENSSL_INSTALL_DIR}\include\openssl dir to your project, and then the above include statement would be:

    #include <ssl.h>
    

    but the former is preferred (recommended)

2. Configure the linker ([MS.Docs]: Linker Options)
Instruct it:

  • Where to search for libraries. You can do that by going to your "Project Properties -> Linker -> General -> Additional Library Directories" and adding ${OPENSSL_INSTALL_DIR}\lib (again, if there are multiple paths, separate them by ;)

  • What libraries to use. "${OPENSSL_INSTALL_DIR}\lib" dir contains a bunch of .lib files. Out of those, you will (most likely) only need ${LIBCRYPTO}.lib and / or ${LIBSSL}.lib. Go to your "Project Properties -> Linker -> Input -> Additional Dependencies" and add those 2 libraries next to the existing ones

3. Build and run

Now, if all your settings and source code are correct, you should have a "buildable" project. When you'll want to run your project output (either an .exe or a .dll needed by another executable, I am not discussing here the possibility of you are using the static libs), the executable will need to find the 2 .dlls that I mentioned at the beginning. For that, you should either:

  • Add their dir to your PATH env var (I consider this the cleanest one). Example (from console): set PATH=%PATH%;${OPENSSL_INSTALL_DIR}

    • For the running VStudio instance:

      Img0

  • Copy them in the folder where your executable is located ([MS.Docs]: Dynamic-Link Library Search Order)

  • Copy them in one of the dirs from your %PATH%.
    Some installers might copy the 2 .dlls in your "%SystemRoot%\System32" dir, and in that case this will no longer be necessary (I find this practice of copying stuff in system dirs a bad one, as in our current example multiple versions can ship the same file names, and the last one installed would overwrite all the others)

Important note: Must be careful when targeting your project for 32bit or 64bit (setting Platform to Win32 or x64 in VStudio IDE) - that has to match your OpenSSL installation architecture.

Solution 2:

A lot has changed since this question was originally asked. Since then vcpkg has come along and has made installing external libraries a lot easier.

To install and use vcpkg, use the following:

git clone  https://github.com/Microsoft/vcpkg
cd vcpkg
bootstrap-vcpkg.bat
vcpkg integrate install

The last command is for optional, but recommended User-wide integration.

Then to install OpenSsl, try the following:

vcpkg search ssl
vcpkg install openssl-windows --triplet x64-windows

Your options may vary depending on the platform specifics.

Restart Visual Studio and you should be ready to go.

Tested with Visual Studio Community 2019.

References:

vcpkg

vcpkg Quick Start

Solution 3:

Use Conan. It is very simple to install and use:

www.conan.io

You can request the files ready for use. For example for Linux x64 or usage with Visual Studio 2012... Here a sample instruction:

conan install OpenSSL/1.0.2g@lasote/stable -s arch="x86_64" -s build_type="Debug" -s compiler="gcc" -s compiler.version="5.3" -s os="Linux" -o 386="False" -o no_asm="False" -o no_rsa="False" -o no_cast="False" -o no_hmac="False" -o no_sse2="False" -o no_zlib="False" ...