简体   繁体   中英

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. At present, I'm using a single .cpp but will export the function to a separate .cpp to be called as needed.

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

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.

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

  1. Find the OpenSSL build you'll be using. For the remainder of this list, that folder will be called OpenSSLDir.
  2. Make sure the SDK in OpenSSLDir matches your Visual Studio build archiecture. If you're using an x86 OpenSSL sdk build your project needs to be x86 as well. Likewise for x64.
  3. Within OpenSSLDir is a family of subfolders: bin, lib, include, etc. Note the include and lib folders, we'll need those.
  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
  5. Within the same project, under Link settings is the "Additional Library Directories", the OpenSSLDir/lib folder should be added there.
  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). If you're also using the SSL_xxx apis, you also need ssleay32.lib on that list.

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. Because OpenSSLDir/include is in the include path, you're halfway there. All OpenSSL headers should be included with the following format :

#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. 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. Ensuring the OpenSSLDir/bin folder is in our path (or startup settings for your Visual Studio debugger) is the easiest way to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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