简体   繁体   English

在 void 函数 C++ 中使用 #include 变量

[英]Using #include variables in void functions C++

I am coming back to C++ after a 15 year hiatus and have been struggling to remember how to make variables/functions from the #include available to void functions.在中断了 15 年之后,我又回到了 C++,并且一直在努力记住如何使 #include 中的变量/函数可用于 void 函数。 At present, I'm using a single .cpp but will export the function to a separate .cpp to be called as needed.目前,我使用的是单个 .cpp,但会将函数导出到单独的 .cpp 以根据需要调用。

I have the following:我有以下几点:

// C++
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <errno.h>
#include <cstring>

// OpenSSL
#include <evp.h>
#include <aes.h>
#include <rand.h>
#include <err.h>
#include <buffer.h>

using namespace std;

typedef struct _cipher_params_t {
    unsigned char *key;
    unsigned char *iv;
    unsigned int encrypt;
    const EVP_CIPHER *cipher_type;
}cipher_params_t;

EVP_CIPHER is in the evp.h file but it doesn't seem to be able to find it. EVP_CIPHER 在 evp.h 文件中,但它似乎无法找到它。 My include path seems to be working as I can see/select the modules when I type #include<> and it finds other variables in other includes (aes.h).我的包含路径似乎正在工作,因为当我输入 #include<> 时我可以看到/选择模块,并且它在其他包含 (aes.h) 中找到其他变量。

Also, in the function below, the items from evp.h cannot not identified such as EVP_CIPHER_CTX *ctx even though it is declared while other items are resolved from other includes such as err.h.此外,在下面的函数中,来自 evp.h 的项目无法识别,例如 EVP_CIPHER_CTX *ctx,即使它被声明而其他项目是从其他包含(例如 err.h)解析的。

 void file_encrypt_decrypt(cipher_params_t *params, FILE *ifp, FILE *ofp) {
 /* Allow enough space in output buffer for additional block */
 int cipher_block_size = EVP_CIPHER_block_size(params->cipher_type);
 unsigned char in_buf[BUFSIZE], out_buf[in_buf + cipher_block_size];

 int num_bytes_read, out_len;
 EVP_CIPHER_CTX *ctx;
 :
 :

I'm using Visual Studio 2017 Enterprise with openSSL.我正在使用带有 openSSL 的 Visual Studio 2017 Enterprise。 Any suggestions?有什么建议? A misstated parameter in the link configuration parameters?链接配置参数中的错误参数?

Any suggestions are greatly appreciated!任何建议都非常感谢! Thanks Rita.谢谢丽塔。

When setting up a Visual Studio project to use an OpenSSL SDK build, you should do the following (forgive me if the names of the Visual Studio settings aren't quite spot on; I don't have dev-studio running in front of me so I'm doing all this from memory):在设置 Visual Studio 项目以使用 OpenSSL SDK 构建时,您应该执行以下操作(如果 Visual Studio 设置的名称不太准确,请原谅我;我没有在我面前运行 dev-studio所以我是凭记忆做这一切的):

  1. Find the OpenSSL build you'll be using.找到您将使用的 OpenSSL 版本。 For the remainder of this list, that folder will be called OpenSSLDir.对于此列表的其余部分,该文件夹将称为 OpenSSLDir。
  2. Make sure the SDK in OpenSSLDir matches your Visual Studio build archiecture.确保 OpenSSLDir 中的 SDK 与您的 Visual Studio 构建架构相匹配。 If you're using an x86 OpenSSL sdk build your project needs to be x86 as well.如果您使用的是 x86 OpenSSL sdk 构建,您的项目也需要是 x86。 Likewise for x64.对于 x64 也是如此。
  3. Within OpenSSLDir is a family of subfolders: bin, lib, include, etc. Note the include and lib folders, we'll need those. OpenSSLDir 中有一系列子文件夹:bin、lib、include 等。注意 include 和 lib 文件夹,我们将需要它们。
  4. Within your Visual Studio project, under C++ Configuration settings (both debug and release build configuration, is an "Additional Include Directories" setting. The full (or relative) path to the OpenSSLDir/include folder should be added there在您的 Visual Studio 项目中,在 C++ 配置设置(调试和发布构建配置)下是一个“附加包含目录”设置。OpenSSLDir/include 文件夹的完整(或相对)路径应该添加到那里
  5. Within the same project, under Link settings is the "Additional Library Directories", the OpenSSLDir/lib folder should be added there.在同一个项目中,在链接设置下是“附加库目录”,应该在那里添加 OpenSSLDir/lib 文件夹。
  6. Within the same project, under Link settings is the "Additional Library Inputs" (this is the one I probably butchered the name/location the worst, but you should be able to find it).在同一个项目中,在链接设置下是“附加库输入”(这是我可能最糟糕的名称/位置,但您应该能够找到它)。 Here you add libeay32.lib to your library file set (should already include kernel32.lib, user32.lib, etc... by inheritance).在这里,您将libeay32.lib添加到您的库文件集(应该已经包含 kernel32.lib、user32.lib 等...通过继承)。 If you're also using the SSL_xxx apis, you also need ssleay32.lib on that list.如果您还使用 SSL_xxx api,则该列表中还需要ssleay32.lib

With all of that in place, then comes your actual usage of the headers and the contained function decls.有了所有这些,接下来就是您对标头和包含的函数声明的实际使用。 Everywhere you use an OpenSSL API should have that API pulled in by the related header.您在任何使用 OpenSSL API 的地方都应该通过相关标头引入该 API。 Because OpenSSLDir/include is in the include path, you're halfway there.因为 OpenSSLDir/include 位于包含路径中,所以您已经完成了一半。 All OpenSSL headers should be included with the following format :所有 OpenSSL 标头都应包含在以下格式中

#include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/aes.h>
... etc ...

The OpenSSL headers frequently (read: guaranteed to happen) includes other headers in the SDK, which are included with exactly the same nomenclature. OpenSSL 标头经常(阅读:保证发生)包括 SDK 中的其他标头,这些标头包含在完全相同的命名法中。 Therefore, it is critical you set it up to work for you (and thus them) the same way.因此,将其设置为以相同的方式为您(以及他们)工作是至关重要的。 Following the instructions I've provided, it should work.按照我提供的说明,它应该可以工作。

I leave the task of ensuring libeay32.dll and ssleay32.dll for your build platform are in the proper path or current working directory when it comes to actually running your program, to you.我将确保构建平台的libeay32.dllssleay32.dll在实际运行程序时位于正确路径或当前工作目录中的任务留给您。 Ensuring the OpenSSLDir/bin folder is in our path (or startup settings for your Visual Studio debugger) is the easiest way to do it.确保 OpenSSLDir/bin 文件夹在我们的路径(或 Visual Studio 调试器的启动设置)中是最简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM