I tried running this sample code from fm4dd.com. But I don't know how to actually include the header files into my program. Orignally it was like:
我尝试从fm4dd.com运行此示例代码。但我不知道如何将头文件实际包含在我的程序中。有点像:
#include <openssl/bio.h>
But i changes it to their actual path, but an error still shows up.
但我将其更改为实际路径,但仍会出现错误。
#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);
}
but the following error shows up every time I try to compile it on the command prompt. Since, I'm a noob, I have no clue how to proceed from here and what to do to fix this error.
但每次我尝试在命令提示符下编译它时,会出现以下错误。因为,我是一个菜鸟,我不知道如何从这里开始,以及如何解决这个错误。
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.
Edit: I included the solution to the problem, but now a new error showed up:
编辑:我包含问题的解决方案,但现在出现了一个新错误:
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
2 个解决方案
#1
1
In many cases, include-files in turn include other files. The paths of these files are specified relative, not absolute. So you have to tell your compiler, where to search for include files in general.
在许多情况下,包含文件又包含其他文件。这些文件的路径是相对的,而不是绝对的。因此,您必须告诉编译器,一般搜索包含文件的位置。
The -I
-option is for this purpose and tells the compiler, which paths (additionally to some standard paths) are to be searched for specified include files, in your case you would use:
-I-option用于此目的并告诉编译器,要搜索指定包含文件的路径(除了一些标准路径),在您使用的情况下,您将使用:
gcc -I C:\openssl\include
If you really need to specify an absolute include path you would use quotes, not <>
, i.e.
如果你真的需要指定绝对包含路径,你会使用引号,而不是<>,即
#include "C:\foo\bar\baz.h"
but if this file includes other files, the compiler will not look specifically into C:\foo\bar
for these.
但如果此文件包含其他文件,编译器将不会专门查看C:\ foo \ bar。
#2
1
Get rid of the full path names in your #include
directives. That is, don't use #include <C:\openssl\include\openssl\bio.h>
; rather, use:
摆脱#include指令中的完整路径名。也就是说,不要使用#include
#include <openssl\bio.h>
#include <openssl\err.h>
#include <openssl\pem.h>
#include <openssl\x509.h>
#include <openssl\e_os2.h>
And pass the include directory to gcc with -I
:
并使用-I将include目录传递给gcc:
gcc -I c:\openssl\include -o myfile myfile.c -lcrypto
#1
1
In many cases, include-files in turn include other files. The paths of these files are specified relative, not absolute. So you have to tell your compiler, where to search for include files in general.
在许多情况下,包含文件又包含其他文件。这些文件的路径是相对的,而不是绝对的。因此,您必须告诉编译器,一般搜索包含文件的位置。
The -I
-option is for this purpose and tells the compiler, which paths (additionally to some standard paths) are to be searched for specified include files, in your case you would use:
-I-option用于此目的并告诉编译器,要搜索指定包含文件的路径(除了一些标准路径),在您使用的情况下,您将使用:
gcc -I C:\openssl\include
If you really need to specify an absolute include path you would use quotes, not <>
, i.e.
如果你真的需要指定绝对包含路径,你会使用引号,而不是<>,即
#include "C:\foo\bar\baz.h"
but if this file includes other files, the compiler will not look specifically into C:\foo\bar
for these.
但如果此文件包含其他文件,编译器将不会专门查看C:\ foo \ bar。
#2
1
Get rid of the full path names in your #include
directives. That is, don't use #include <C:\openssl\include\openssl\bio.h>
; rather, use:
摆脱#include指令中的完整路径名。也就是说,不要使用#include
#include <openssl\bio.h>
#include <openssl\err.h>
#include <openssl\pem.h>
#include <openssl\x509.h>
#include <openssl\e_os2.h>
And pass the include directory to gcc with -I
:
并使用-I将include目录传递给gcc:
gcc -I c:\openssl\include -o myfile myfile.c -lcrypto