2

I have the following code in the file rsatest.c. I'm trying to generate a 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);
    return 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?

jww
  • 97,681
  • 90
  • 411
  • 885
Sashank
  • 107
  • 2
  • 11

1 Answers1

2

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.

Marek Klein
  • 1,410
  • 12
  • 20
  • @Sashank does it look like this now: `gcc -I../include/ rsatest.c -L . -lcrypto -lssl -lz -lgdi32`? – Marek Klein Nov 17 '16 at 09:53
  • Well I used `gcc -I../include/ rsatest.c -lz -lgdi32 -L . -lcrypto -lssl`, because lz and lgdi32 were not present in the folder where rsatest.c was. But your edit works, and it compiles. And I realized that crypto gets its definitions from z and gdi, and it does not matter where -L is. So even this works `gcc -I../include/ rsatest.c -lcrypto -lssl -lz -lgdi32 -L .` . Thanks! – Sashank Nov 17 '16 at 10:19