我今天开始研究用于 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 to
dlopen' dso_dlfcn.c:(.text+0x33): undefined reference todlsym' dso_dlfcn.c:(.text+0x3d): undefined reference to
dlclose' /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In functiondlfcn_bind_func': dso_dlfcn.c:(.text+0x3b1): undefined reference to
dlsym' dso_dlfcn.c:(.text+0x490): undefined reference todlerror' /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function
dlfcn_bind_var': dso_dlfcn.c:(.text+0x511): undefined reference todlsym' dso_dlfcn.c:(.text+0x5f0): undefined reference to
dlerror' /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In functiondlfcn_load': dso_dlfcn.c:(.text+0x667): undefined reference to
dlopen' dso_dlfcn.c:(.text+0x6de): undefined reference todlclose' dso_dlfcn.c:(.text+0x715): undefined reference to
dlerror' /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In functiondlfcn_pathbyaddr': dso_dlfcn.c:(.text+0x7b1): undefined reference to
dladdr' dso_dlfcn.c:(.text+0x819): undefined reference todlerror' /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function
dlfcn_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/