C++ 如何在 Visual Studio 中包含 OpenSSL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32156336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:06:03  来源:igfitidea点击:

How to include OpenSSL in Visual Studio

c++visual-studiovisual-studio-2012openssl

提问by jikama

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.

我很难将 openssl 添加到我的项目中。我已经下载了 Windows 的预编译安装程序,并且安装了库,但是我找不到在我的项目中包含 openssl 的方法。

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

注意:我在 Windows 7 x64 上使用 Visual Studio Expres 2012,但它不限于该环境。

回答by CristiFati

Intro

介绍





Let's assume you have installed OpenSSLin a dirlike: "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 and files, out of which matter for us:

假设您已经在如下目录中安装了OpenSSL“C:\Program Files\Openssl-Win32-1.0.1p.....”(或任何其他名称);我将把它称为OPENSSL_INSTALL_DIR(因为它是一个环境变量)。所以%OPENSSL_INSTALL_DIR%应该包含一堆dir和文件,这对我们来说很重要:

  • Dirs:

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

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


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

    • v1.0.2and 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*
  • 目录:

    • 包括
  • 文件(因为它们的名称因版本而异,我将使用(Nix样式var)占位符来引用它们;它们也可能位于binsubdir 中):

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


    其中LIBCRYPTO( #1.) 和LIBSSL( #2.) 定义如下:

    • v1.0.2及更早版本

      1. libeay32
      2. ssleay32(也可能被复制(或符号链接)到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 VStudioproject you have to:

为了使用它,在你的VStudio项目中你必须:

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

配置编译器[MS.Docs]:按字母顺序列出的编译器选项
指示它:

  • Whereto 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 OpenSSLheader files.
    Note that because "%OPENSSL_INSTALL_DIR%\include" dircontains an opensslsubdir 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\openssldirto your project, and then the above include statement would be:

    #include <ssl.h>
    

    but the former is preferred (recommended)

  • 在哪里搜索包含(头文件 ( .h))文件。转到您的“项目属性 -> C/C++ -> 常规 -> 附加包含目录”并添加%OPENSSL_INSTALL_DIR%\include(如果您需要添加其他路径,请用分号 ( ;)分隔它们)。现在您可以在源代码中包含OpenSSL头文件。
    请注意,因为“ %OPENSSL_INSTALL_DIR%\include目录包含一个openssl子目录,并且在该子目录下是实际的头文件,您的#include子句必须如下所示:

    #include <openssl/ssl.h>
    

    当然,您可以将%OPENSSL_INSTALL_DIR%\include\openssl目录添加到您的项目中,然后上面的 include 语句将是:

    #include <ssl.h>
    

    但前者是首选(推荐)

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

配置链接器( [MS.Docs]: Linker Options)
指示它:

  • Whereto 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 ;)

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

  • 在哪里搜索图书馆。您可以通过转到“项目属性 -> 链接器 -> 常规 -> 附加库目录”并添加%OPENSSL_INSTALL_DIR%\lib(同样,如果有多个路径,请用;分隔它们)

  • 使用什么库。“ %OPENSSL_INSTALL_DIR%\lib目录包含一堆.lib文件。其中,您(很可能)只需要${LIBCRYPTO}.lib和/或${LIBSSL}.lib。转到“项目属性 -> 链接器 -> 输入 -> 附加依赖项”并将这两个库添加到现有库旁边

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

现在,如果您的所有设置和源代码都是正确的,您应该有一个“可构建”的项目。当您想要运行项目输出(另一个可执行文件需要的.exe.dll文件,我在这里不讨论您使用静态lib的可能性)时,可执行文件需要找到 2 .我在开头提到的dll。对于这一点,你应该要么

  • 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%envvar

  • Add their dirto your %PATH%var.
    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)

  • 将它们复制到您的可执行文件所在的文件夹中([MS.Docs]:动态链接库搜索顺序

  • 将它们复制到%PATH% env var中的文件夹之一

  • 将他们的目录添加到您的%PATH% var
    一些安装程序可能会在您的“ %SystemRoot%\System32目录中复制 2 个.dlls ,在这种情况下,这将不再是必要的(我发现这种在系统目录中复制内容的做法很糟糕,就像我们当前的例子一样多个版本可以提供相同的文件名,最后一个安装会覆盖所有其他版本)

Important note: Must be careful when targeting your project for 32bitor 64bit(setting Platformto Win32or x64in VStudio IDE) - that has to match your OpenSSLinstallation architecture.

重要提示:在将项目定位为32 位64 位时必须小心(在VStudio IDE中将平台设置为Win32x64) - 这必须与您的OpenSSL安装架构相匹配。

回答by Alejandro PC

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

使用柯南。安装和使用非常简单:

www.conan.io

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:

您可以请求准备好使用的文件。例如对于 Linux x64 或与 Visual Studio 2012 一起使用...这里有一个示例指令:

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" ...