Openssl for RSA: undefined reference to RSA_new

Today I am starting to learn openSSL api for RSA. What a simple code:

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

and I compile with

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

but gcc returns an error undefining references to RSA_new and RSA_free. I am checking the rsa.h header and there is no link to these two functions. what's wrong? I am following the reference guide on the openssl website ...

EDIT: gcc Output:

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 link to dlsym' dso_dlfcn.c:(.text+0x3d): undefined reference to dcl '/usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In the dlfcn_bind_func': dso_dlfcn.c:(.text+0x3b1): undefined reference to function dlfcn_bind_func': dso_dlfcn.c:(.text+0x3b1): undefined reference to dlsym 'dso_dlfcn.c :(. text + 0x490) : undefined link to dlerror' /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function dlfcn_bind_var': dso_dlfcn.c :(. text + 0x511): undefined link to dlsym' dso_dlfcn.c:(.text+0x5f0): undefined reference to dlerror '/usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In the dlfcn_load': dso_dlfcn.c:(.text+0x667): undefined reference to function dlfcn_load': dso_dlfcn.c:(.text+0x667): undefined reference to dlopen ' dso_dlfcn.c :(. text + 0x6de): undefined link to dlclose' dso_dlfcn.c:(.text+0x715): undefined reference to dlclose' dso_dlfcn.c:(.text+0x715): undefined reference to dlerror '/usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In the dlfcn_pathbyaddr': dso_dlfcn.c:(.text+0x7b1): undefined reference to function dlfcn_pathbyaddr': dso_dlfcn.c:(.text+0x7b1): undefined reference to dladdr 'dso_dlfcn.c(. text + 0x819): undefined link to dlerror' /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function dlfcn_unload': dso_dlfcn.c :(. text + 0x87a): undefined link to `dlclose ' collect2: ld returned 1 exit status

+6
source share
5 answers

The propaganda is that you are libssl and you are using RSA cryptography, which is part of libcrypto , another error: no function: RSA_new_ :

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

So fix your code:

 rsa = RSA_new(); 

And compile this:

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

EDIT: for the last error (dl function):

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

You need to add -lssl or something like that. An 'undefined reference' is generated by the linker, which looks for the actual implementation of RSA_new and RSA_free . These functions are located somewhere in the openssl library, and with -lssl you let the linker know where they are.

EDIT: if there is something wrong in the header file, you will see an error, for example, "implicit declaration of the RSA_new identifier". But you need to enable certain flags with such errors (I thought -Wmissing-prototypes ).

+2
source

You also need to establish a connection with the library:

 gcc -I/usr/local/ssl/include -o etc etc.c -L/usr/local/lib -lssl 

The -L option tells GCC where to look for the library file, and -L (small L) tells the linker that it should reference the library.

Replace the library folder and library name with what you received.

+2
source

You need to link the libSSL library. Sort of

 gcc -I /usr/local/ssl/include -o myprog myprog.c -lssl 

will do the trick.

(Maybe this is not really -lssl , but -lopenssl , -lssl-rsa or something else, you can find this by typing

 pkg-config --libs PACKAGENAME 

where PACKAGENAME is the name of the package containing libssl, something like libssl, openssl, libssl-dev, openssl-devel, etc.)

+1
source

If you are using CMakeLists.txt, you should add this simple command:

 link_libraries(ssl crypto) 
0
source

Source: https://habr.com/ru/post/923135/


All Articles