Sashank Sashank - 17 days ago 5
C Question

Undefined reference to RSA_generate_key in open ssl?

I have the following code in the file rsatest.c. I'm trying to generate an rsa key pair.

#include <openssl/rsa.h>
#include <openssl/pem.h>

int main(){
RSA *rsa = RSA_generate_key((const int) 1024,(const int) 3, 0, 0);
}


I'm compiling this with

gcc -I../include/ -L . -lcrypto -lssl rsatest.c


and I get the following error.

undefined reference to `RSA_generate_key'


Am I linking the library files in the wrong order? I made the libcrypto.a and libssl.a on windows (64 bit), with msys and mingw, and I'm running the code on the same system.

RSA_generate_key has been declared in rsa.h. Is it not defined in libcrypto.a?

EDIT :

I tried this too,

gcc -I../include rsatest.c -L . -lcrypto -lssl


and I understand that the linker will look for definitions in the libraries going from left to right.

However, I get new undefined references to various functions in

rand_win.o and c_zlib.o


I looked up online and found the missing symbols in the libraries gdi32 and zlib. So I added

-lz and -lgdi32


The compiler did not complain about a missing library, so I assume they are present with mingw. And still, I get the same output.

I also used nm, and found that the symbols were indeed undefined in rand_win.o and c_zlib.o.

Why cant the linker seem to find definitions in these libraries?

Answer

Change the order in your gcc command.

gcc -I../include/ rsatest.c -L . -lcrypto -lssl

As far as I know linker has a list of undefined symbols. When it processes libcrypto.a and libssl.a it does not have anything in the list of undefined symbols so he just drops the libraries. Then after processing rsatest it has something in its list but it does not look for symbols in already processed libraries.

Comments