问题
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.
回答1:
Let's assume you have installed OpenSSL in a folder 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 env var). So %OPENSSL_INSTALL_DIR% should contain a bunch of dirs (folders) and files, out of which matter for us:
Dirs:
- include
- lib
Files:
- libeay32.dll
- ssleay32.dll (might be also copied (or symlinked) to libssl32.dll)
In order to make use of it, in your VStudio project you have to:
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#includeclauses 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 first form is the preferred one
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 libeay32.lib and / or ssleay32.lib. Go to your "Project Properties -> Linker -> Input -> Additional Dependencies" and add those 2 libraries next to the existing ones
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:
- Copy them in the folder where your executable is located ([MS.Docs]: Dynamic-Link Library Search Order)
- Copy them in one of the folders from your %PATH% env var
- Add %OPENSSL_INSTALL_DIR% to your %PATH% variable; although if I remember correctly the installer also copies them in your "%SystemRoot%\System32" dir and in that case you won't have to do this step, but that's just a heads up
Important note: Must be careful when targeting your project for 32 or 64 bit (setting Platform to Win32 or x64 in VStudio IDE); that has to match your OpenSSL installation architecture.
回答2:
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" ...
来源:https://stackoverflow.com/questions/32156336/how-to-include-openssl-in-visual-studio