Undefined reference to RSA_generate_key in OpenSSL? [duplicate]

为君一笑 提交于 2019-12-02 02:06:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/40649685/undefined-reference-to-rsa-generate-key-in-openssl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!