我今天开始探索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;
}
Run Code Online (Sandbox Code Playgroud)
我正在编译
gcc -I/usr/local/ssl/include -o等等
但是gcc返回了对RSA_new和RSA_free的未定义引用的错误.我检查了rsa.h头文件,并没有引用这两个函数.怎么了?我遵循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):功能
dlfcn_globallookup': dso_dlfcn.c:(.text+0x1d): undefined reference to
dlopen'dso_dlfcn.c :(.text + 0x33):对dlsym' dso_dlfcn.c:(.text+0x3d): undefined reference to
dlclose的未定义引用'/usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o):在函数dlfcn_bind_func': dso_dlfcn.c:(.text+0x3b1): undefined reference to
dlsym'dso_dlfcn.c :(.text + 0x490) :对dlerror' /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function
dlfcn_bind_var的未定义引用':dso_dlfcn.c :(.text + 0x511):对dlsym' dso_dlfcn.c:(.text+0x5f0): undefined reference to
dlerror'/dlfcn_load': dso_dlfcn.c:(.text+0x667): undefined reference to
usr /local/ ssl/lib/libcrypto.a(dso_dlfcn.o)的未定义引用:在函数dlopen'dso_dlfcn.c :( .text + 0x6de):对dlclose' dso_dlfcn.c:(.text+0x715): undefined reference to
dlerror'/dlfcn_pathbyaddr': dso_dlfcn.c:(.text+0x7b1): undefined reference to
usr /local/ ssl/lib/libcrypto.a(dso_dlfcn.o)的未定义引用:在函数dladdr'dso_dlfcn.c :(.text + 0x819)中:未定义引用dlerror' /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function
dlfcn_unload': dso_dlfcn.c :(.text + 0x87a):未定义引用`dlclose'colle2:ld返回1退出状态
问题是您正在链接libssl
并且您正在使用 RSA 加密,这是libcrypto
另一个错误的一部分:没有调用 :: 的函数RSA_new_
:
toc@UnixServer:/usr/include/openssl$ grep RSA_new *
rsa.h:RSA * RSA_new(void);
rsa.h:RSA * RSA_new_method(ENGINE *engine);
Run Code Online (Sandbox Code Playgroud)
所以更正你的代码:
rsa = RSA_new();
Run Code Online (Sandbox Code Playgroud)
并像这样编译:
gcc -I/usr/include/openssl/ -Wall my_rsa.c -o my_rsa -lcrypto
Run Code Online (Sandbox Code Playgroud)
编辑:对于最后一个错误(dl 函数):
gcc -I/usr/include/openssl/ -Wall my_rsa.c -o my_rsa -lcrypto -ldl
Run Code Online (Sandbox Code Playgroud)