c - OpenSSL 中未定义对 RSA_generate_key 的引用?

标签 c windows openssl mingw undefined-reference

我在文件rsatest.c中有以下代码。我正在尝试生成 RSA key 对。

#include <openssl/rsa.h>
#include <openssl/pem.h>

int main(){
    RSA *rsa = RSA_generate_key((const int) 1024,(const int) 3, 0, 0);
    return 0;
}

我正在编译这个

gcc -I../include/ -L . -lcrypto -lssl rsatest.c

我收到以下错误。

 undefined reference to `RSA_generate_key'

我链接库文件的顺序是否错误?我在 Windows(64 位)上使用 msys 和 mingw 制作了 libcrypto.a 和 libssl.a,并且我在同一系统上运行代码。

RSA_generate_key 已在 rsa.h 中声明。 libcrypto.a 中没有定义吗?

编辑:

我也尝试过这个,

gcc -I../include rsatest.c -L . -lcrypto -lssl

据我了解,链接器将从左到右在库中查找定义。

但是,我在

中获得了对各种函数的新的 undefined reference
rand_win.o and c_zlib.o

我在网上查了一下,发现 gdi32 和 zlib 库中缺少符号。所以我添加了

-lz and -lgdi32

编译器没有提示缺少库,所以我假设它们与 mingw 一起存在。尽管如此,我还是得到了相同的输出。

我也使用了nm,发现rand_win.o和c_zlib.o中的符号确实是未定义的。

为什么链接器似乎无法在这些库中找到定义?

最佳答案

更改 gcc 命令中的顺序。

gcc -I../include/ rsatest.c -L . -lcrypto -lssl

据我所知链接器有一个 undefined symbol 的列表。当它处理 libcrypto.a 和 libssl.a 时, undefined symbol 列表中没有任何内容,因此他只是删除这些库。然后,在处理 rsatest 后,它的列表中有一些内容,但它不会在已处理的库中查找符号。

关于c - OpenSSL 中未定义对 RSA_generate_key 的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40649685/

相关文章:

c - 解决多重包含问题

c# - 在 Window Phone 8.1 中从服务器收到原始推送通知后执行某些功能

c - 这两个循环中哪个更快?

openssl - 无法打开 Perl 脚本 "Configure": no such file or directory

c - 使用指向结构的指针丢失内存字节

c - 在自定义 malloc 中附加调试 header

windows - 如何将下载的 .box 文件添加到 Vagrant?

python - 如何解决使用 PROTOCOL_TLSv1_2 的 SSL 错误?

openssl - Mac 验证错误 : invalid password? 使用 openssl pkcs12 将 .p12 转换为 .pem

c - MING文件运行错误