giozh
giozh

Reputation: 10068

Openssl for RSA: undefined reference to RSA_new

I've starto today to explore openSSL api for RSA. That's the 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'm compiling with

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

but gcc return error of undefining reference to RSA_new and RSA_free. I've check the rsa.h header, and there's no reference to this two function. what's wrong? I've follow the reference guide on 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 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

Upvotes: 11

Views: 16561

Answers (6)

Fermi-4
Fermi-4

Reputation: 729

You can also try this:

gcc <other args> -lcrypto

Which tells the compiler to include and link with the crypto header files.

This works if you are using openssl i.e. #include <openssl/bn.h>

Upvotes: 1

Yegor_Ch
Yegor_Ch

Reputation: 1

If you use CMakeLists.txt, you must add this simple command:

link_libraries(ssl  crypto)

Upvotes: 0

TOC
TOC

Reputation: 4446

The propblem is that you are linking with libssl and you are using RSA crypto which is part of libcrypto, another error : there is no function called : 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 correct your code:

rsa = RSA_new();

And compile like that:

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

EDIT : for the last error(dl functions):

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

Upvotes: 5

Some programmer dude
Some programmer dude

Reputation: 409176

You need to link with the library as well:

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

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

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

Upvotes: 3

Bart
Bart

Reputation: 1653

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

EDIT: if there would be something wrong with the header file you would see an error like 'implicit declaration of identifier RSA_new'. But you need to enable certain flags to have that kind of errors (I thought -Wmissing-prototypes).

Upvotes: 2

user529758
user529758

Reputation:

You have to link against the libSSL library. Something like

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

will do the trick.

(Maybe it's not actually -lssl but -lopenssl, -lssl-rsa or whatever; you can find this out by typing

pkg-config --libs PACKAGENAME

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

Upvotes: 2

Related Questions