Search code examples
c++cvisual-studiovisual-studio-2012openssl

How to include OpenSSL in Visual Studio


I am having a hard time trying to add openssl to my project. I have downloaded the precompiled installer for windows, and I have the libraries installed, but I can't find a way to include openssl in my project.

Note: I am using Visual Studio Expres 2012 on Windows 7 x64, but it's not restricted to that environment.


Solution

  • Intro



    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):

      1. ${LIBCRYPTO}.dll

      2. ${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.*

        1. libcrypto-3*(-x64)

        2. libssl-3*(-x64)

    As a side note, on Nix, the 2 files (.dlls - shared objects) are typically named libcrypto.so.* and libssl.so.* (where suffix is a version indicator).

    In order to make use of OpenSSL installation, in your VStudio project you have to (check [SO]: LNK2005 Error in CLR Windows Form (@CristiFati's answer) for more details on the rationale (the why part)):

    1. Configure the compiler ([MS.Learn]: 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.Learn]: 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), the executable will need to find the 2 .dlls that I mentioned at the beginning (if you linked with the _static versions (check #1), this doesn't apply (isn't required)). For that, you should either:

    1. Add their dir to your PATH env var (I consider this the cleanest one). Example:

      • From console (before launching the executable):

        set PATH=%PATH%;${OPENSSL_INSTALL_DIR}
        
      • For the running VStudio instance:

        Img0

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

    3. 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 032bit or 064bit (setting Platform to Win32 or x64 in VStudio IDE) - that has to match your OpenSSL installation architecture (check [SO]: Python Ctypes - loading dll throws OSError: [WinError 193] %1 is not a valid Win32 application (@CristiFati's answer) for (funky) errors that occur if it doesn't).

    Worth mentioning that .dll search mechanism also applies when the .dll is dynamically loaded (via [MS.Learn]: LoadLibraryExW function (libloaderapi.h) functions family), as explained in the above URL. There are also other ways (but those include hardocding paths: [MS.Learn]: AddDllDirectory function (libloaderapi.h)). Although in a more complex scenario, [SO]: Can't import dll module in Python (@CristiFati's answer) covers them.