c - 如何使用 MinGW 在 Windows 上使用 OpenSSL 编译程序

标签 c windows gcc openssl mingw

我尝试从 fm4dd.com 运行此示例代码。但我不知道如何将头文件包含到我的程序中。 最初是这样的:

#include <openssl/bio.h>

但是我改成他们的实际路径,还是报错

#include <C:\openssl\include\openssl\bio.h>
#include <C:\openssl\include\openssl\err.h>
#include <C:\openssl\include\openssl\pem.h>
#include <C:\openssl\include\openssl\x509.h>
#include <C:\openssl\include\openssl\e_os2.h>

int main() {

  const char cert_filestr[] = "./cert-file.pem";
             EVP_PKEY *pkey = NULL;
  BIO              *certbio = NULL;
  BIO               *outbio = NULL;
  X509                *cert = NULL;
  int ret;

  /* ---------------------------------------------------------- *
   * These function calls initialize openssl for correct work.  *
   * ---------------------------------------------------------- */
  OpenSSL_add_all_algorithms();
  ERR_load_BIO_strings();
  ERR_load_crypto_strings();

  /* ---------------------------------------------------------- *
   * Create the Input/Output BIO's.                             *
   * ---------------------------------------------------------- */
  certbio = BIO_new(BIO_s_file());
  outbio  = BIO_new_fp(stdout, BIO_NOCLOSE);

  /* ---------------------------------------------------------- *
   * Load the certificate from file (PEM).                      *
   * ---------------------------------------------------------- */
  ret = BIO_read_filename(certbio, cert_filestr);
  if (! (cert = PEM_read_bio_X509(certbio, NULL, 0, NULL))) {
    BIO_printf(outbio, "Error loading cert into memory\n");
    exit(-1);
  }

  /* ---------------------------------------------------------- *
   * Extract the certificate's public key data.                 *
   * ---------------------------------------------------------- */
  if ((pkey = X509_get_pubkey(cert)) == NULL)
    BIO_printf(outbio, "Error getting public key from certificate");

  /* ---------------------------------------------------------- *
   * Print the public key information and the key in PEM format *
   * ---------------------------------------------------------- */
  /* display the key type and size here */
  if (pkey) {
    switch (pkey->type) {
      case EVP_PKEY_RSA:
        BIO_printf(outbio, "%d bit RSA Key\n\n", EVP_PKEY_bits(pkey));
        break;
      case EVP_PKEY_DSA:
        BIO_printf(outbio, "%d bit DSA Key\n\n", EVP_PKEY_bits(pkey));
        break;
      default:
        BIO_printf(outbio, "%d bit non-RSA/DSA Key\n\n", EVP_PKEY_bits(pkey));
        break;
    }
  }

  if(!PEM_write_bio_PUBKEY(outbio, pkey))
    BIO_printf(outbio, "Error writing public key data in PEM format");

  EVP_PKEY_free(pkey);
  X509_free(cert);
  BIO_free_all(certbio);
  BIO_free_all(outbio);
  exit(0);
}

但是每次我尝试在命令提示符下编译它时都会出现以下错误。因为我是菜鸟,所以我不知道如何从这里开始以及如何修复此错误。

c:\openssl>gcc -lssl -lcrypto -o test test.c 
In file included from test.c:1:0:
C:\openssl\include\openssl\bio.h:62:27: fatal error: openssl/e_os2.h: No such file or directory
#include <openssl/e_os2.h>
                       ^
compilation terminated.

编辑: 我包含了问题的解决方案,但现在出现了一个新错误:

c:\openssl>gcc -lssl -lcrypto -o test test.c -IC:\openssl\include\
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -lssl
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -lcrypto
collect2.exe: error: ld returned 1 exit status

最佳答案

在许多情况下,包含文件又包含其他文件。这些文件的路径是相对指定的,而不是绝对的。所以你必须告诉你的编译器,一般在哪里搜索包含文件。

-I -option 用于此目的并告诉编译器要搜索哪些路径(除了一些标准路径之外)以查找指定的包含文件,在您的情况下您将使用:

gcc -I C:\openssl\include

如果您确实需要指定一个绝对包含路径,您可以使用引号,而不是 <> ,即

#include "C:\foo\bar\baz.h"

但如果此文件包含其他文件,编译器将不会专门查看 C:\foo\bar对于这些。

关于c - 如何使用 MinGW 在 Windows 上使用 OpenSSL 编译程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34751049/

相关文章:

windows - 在 IntelliJ Community Edition 和 Tomcat8 上调试 J2EE 应用程序(作为服务运行)

c - 求串口读取例子

xcode - 为什么 Homebrew 报告 "couldn' t 理解 kern.osversion `14.0.0'”?

c++ - gcc和c++ 17的重载解析失败

在 C 中从 HEX 颜色转换为 RGB 结构

c - 在 C 中使用 dlopen 时,是否有避免 dlsym 的优雅方法?

regex - 使用 grep 在多个文件中查找字符串

c - 为枚举变量分配一个超出枚举范围的值时是否有警告?

c - GNU Make 构建过时且更新于特定时间戳的文件

c - 为以下密码/字母谜题寻找强力算法