c - 用于 RSA 的 OpenSSL : undefined reference to RSA_new

标签 c unix openssl

我今天开始研究用于 RSA 的 openSSL api。这是简单的代码:

#include<stdio.h>
#include<openssl/rsa.h>
#include<openssl/engine.h>
int main() {
    RSA *rsa;
    rsa = RSA_new_();
    RSA_free(rsa);
    return 0;
}

我正在编译

gcc -I /usr/local/ssl/include -o etc etc

但是 gcc 返回未定义对 RSA_new 和 RSA_free 的引用的错误。我检查了 rsa.h header ,没有提到这两个函数。 怎么了? 我已经按照 openssl 网站上的引用指南...

编辑: gcc 输出:

gcc -I /usr/local/ssl/include/ -o rsa rsa.c -L/usr/local/ssl/lib -lcrypto /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function dlfcn_globallookup': dso_dlfcn.c:(.text+0x1d): undefined reference todlopen' dso_dlfcn.c:(.text+0x33): undefined reference to dlsym' dso_dlfcn.c:(.text+0x3d): undefined reference todlclose' /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function dlfcn_bind_func': dso_dlfcn.c:(.text+0x3b1): undefined reference todlsym' dso_dlfcn.c:(.text+0x490): undefined reference to dlerror' /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In functiondlfcn_bind_var': dso_dlfcn.c:(.text+0x511): undefined reference to dlsym' dso_dlfcn.c:(.text+0x5f0): undefined reference todlerror' /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function dlfcn_load': dso_dlfcn.c:(.text+0x667): undefined reference todlopen' dso_dlfcn.c:(.text+0x6de): undefined reference to dlclose' dso_dlfcn.c:(.text+0x715): undefined reference todlerror' /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function dlfcn_pathbyaddr': dso_dlfcn.c:(.text+0x7b1): undefined reference todladdr' dso_dlfcn.c:(.text+0x819): undefined reference to dlerror' /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In functiondlfcn_unload': dso_dlfcn.c:(.text+0x87a): undefined reference to `dlclose' collect2: ld returned 1 exit status

最佳答案

问题是您正在链接 libssl 并且您使用的是 libcrypto 的一部分的 RSA 加密,另一个错误:没有函数调用: RSA_new_:

toc@UnixServer:/usr/include/openssl$ grep RSA_new *
rsa.h:RSA * RSA_new(void);
rsa.h:RSA * RSA_new_method(ENGINE *engine);

因此更正您的代码:

rsa = RSA_new();

然后这样编译:

gcc -I/usr/include/openssl/ -Wall my_rsa.c -o my_rsa  -lcrypto

编辑:对于最后一个错误(dl 函数):

gcc -I/usr/include/openssl/ -Wall my_rsa.c -o my_rsa  -lcrypto -ldl

关于c - 用于 RSA 的 OpenSSL : undefined reference to RSA_new,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12008954/

相关文章:

c++ - 素描圆柱体

regex - BASH 中的 IF 语句没有达到预期的效果

python - 如何从命令行将 EOF 发送到 Python sys.stdin? CTRL-D 不起作用

python - M2Crypto 没有安装在 venv 中,或者 swig 没有定义 __x86_64__ 这会破坏针对 OpenSSL 的编译

c# - .NET Core - HTTPClient - Ubuntu 20.04 上的 dh key 太小

c - C 中这些函数指针之间的转换安全吗?

c - 是否正在评估指向 undefined object 存储之外的指针?

c++ - 并行计算——输出困惑?

C套接字编程: calling recv() changes my socket file descriptor?

Ruby 从预先存在的 key 字符串创建密码 key