Openssl for RSA:对RSA_new的未定义引用

gio*_*ozh 6 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;
}
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 todlopen'dso_dlfcn.c :(.text + 0x33):对dlsym' dso_dlfcn.c:(.text+0x3d): undefined reference todlclose的未定义引用'/usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o):在函数dlfcn_bind_func': dso_dlfcn.c:(.text+0x3b1): undefined reference todlsym'dso_dlfcn.c :(.text + 0x490) :对dlerror' /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In functiondlfcn_bind_var的未定义引用':dso_dlfcn.c :(.text + 0x511):对dlsym' dso_dlfcn.c:(.text+0x5f0): undefined reference todlerror'/ dlfcn_load': dso_dlfcn.c:(.text+0x667): undefined reference tousr /local/ ssl/lib/libcrypto.a(dso_dlfcn.o)的未定义引用:在函数dlopen'dso_dlfcn.c :( .text + 0x6de):对dlclose' dso_dlfcn.c:(.text+0x715): undefined reference todlerror'/ dlfcn_pathbyaddr': dso_dlfcn.c:(.text+0x7b1): undefined reference tousr /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 functiondlfcn_unload': dso_dlfcn.c :(.text + 0x87a):未定义引用`dlclose'colle2:ld返回1退出状态

TOC*_*TOC 5

问题是您正在链接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)