3

I am having trouble compiling and linking a program using OpenSSL under MinGW and a C++ compiler. These are my headers:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <openssl/rsa.h>
#include <openssl/engine.h>
#include <openssl/pem.h>

UPDATED (CHECK THE COMMENTS) :

I want compile it using MinGW so I first tried :

i686-w64-mingw32-g++ -o crypto_file_example base64.h en.cpp -lssl -lcrypto

But I got :

en.cpp:5:25: fatal error: openssl/rsa.h: No such file or directory
compilation terminated.

so i tried :

i686-w64-mingw32-g++ -o crypto_file_example base64.h en.cpp -I/usr/local/openssl/include/ /usr/local/openssl/lib/libssl.a /usr/local/openssl/lib/libcrypto.a /usr/lib/x86_64-linux-gnu/libdl.a

And here's another error came out :

 /tmp/ccZGvNhw.o:en.cpp:(.text+0x5ec): undefined reference to `BIO_new_mem_buf'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x602): undefined reference to `BIO_set_flags'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x625): undefined reference to `PEM_read_bio_RSA_PUBKEY'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x633): undefined reference to `ERR_get_error'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x643): undefined reference to `ERR_error_string'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x65e): undefined reference to `BIO_free'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x67c): undefined reference to `BIO_new_mem_buf'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x6a2): undefined reference to `PEM_read_bio_RSAPrivateKey'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x6b0): undefined reference to `ERR_get_error'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x6c0): undefined reference to `ERR_error_string'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x6db): undefined reference to `BIO_free'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x6f1): undefined reference to `RSA_size'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x72a): undefined reference to `RSA_public_encrypt'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x740): undefined reference to `ERR_get_error'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x750): undefined reference to `ERR_error_string'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x776): undefined reference to `RSA_size'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x792): undefined reference to `RSA_size'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x7b9): undefined reference to `RSA_private_decrypt'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x7cf): undefined reference to `ERR_get_error'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x7df): undefined reference to `ERR_error_string'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x918): undefined reference to `OPENSSL_init_crypto'
/tmp/ccZGvNhw.o:en.cpp:(.text+0x9a4): undefined reference to `RSA_free'
/tmp/ccZGvNhw.o:en.cpp:(.text+0xa4c): undefined reference to `RSA_free'
collect2: error: ld returned 1 exit status

And by the way I installed OpenSSL by downloading it from openssl website and compiling it !

So any help ? Or new ideas to try ?

jww
  • 97,681
  • 90
  • 411
  • 885
MeNotMe
  • 147
  • 9
  • The error you get when executing your compiled program seems like its missing the the shared libraries to run. Can they be found within the LD_LIBRARY_PATH ? – Marian Aug 28 '16 at 20:19
  • @Taredon what should put in the LD_LIBRARY_PATH ? – MeNotMe Aug 28 '16 at 21:25
  • `OPENSSL_init_crypto` is from OpenSSL 1.1.0. It sounds like you are loading the wrong version of the libraries at runtime. Try compiling with `g++ -I/usr/local/openssl/include/ /usr/local/openssl/lib/libssl.a /usr/local/openssl/lib/libcrypto.a -o crypto_file_example base64.h en.cpp`. An archive is just a collection of object files, so you can use it where object files are used. It also avoids the problems with `-L` and `-l`. – jww Aug 28 '16 at 22:21
  • @jww I got these errors : http://pastebin.com/raw/p8kdTuMC :/ – MeNotMe Aug 29 '16 at 00:39
  • @MeNotMe - *`undefined reference to 'dlopen'`* - add `-ldl` to you compile/link command. – jww Aug 29 '16 at 00:50
  • @jww Very good now for g++ we are good ! but for i686-w64-mingw32-g++ i'm running this `i686-w64-mingw32-g++ -o crypto_file_example base64.h en.cpp -I/usr/local/openssl/include/ /usr/local/openssl/lib/libssl.a /usr/local/openssl/lib/libcrypto.a /usr/lib/x86_64-linux-gnu/libdl.a -pthread` and i'm getting these errors : http://pastebin.com/raw/BS0gZH8M – MeNotMe Aug 29 '16 at 04:13
  • @MeNotMe - Don't know... I'm guessing (and its only a guess), `i686-w64-mingw32-g++` is 32-bit and trying to produce 32-bit executables, but you built 64-bit OpenSSL libraries. – jww Aug 29 '16 at 05:30
  • @jww good idea, I tried that and this was the result no errors but only `collect2: error: ld returned 1 exit status` – MeNotMe Aug 29 '16 at 19:55
  • compiling `base64.h` is possibly an error., normally `.h` files are included and not directly compiled – M.M Aug 29 '16 at 22:41
  • @MeNotMe there is always error messages before `ld returned 1 exit status` – M.M Aug 29 '16 at 22:42
  • @MeNotMe did you build openssl with the same compiler? – M.M Aug 29 '16 at 22:42
  • Hey guys ! new progress here ! i downloaded precompiled openssl libs for windows and these are the new errors : http://pastebin.com/raw/29JtgrT0 @M.M yep same compiler but look at that – MeNotMe Aug 30 '16 at 00:11
  • Oops sorry guys I used Gcc instead of G++ i just fixed and i think it works .. wait for the tests – MeNotMe Aug 30 '16 at 00:14
  • I think everyuthing is fine i just when i run the program using wine i got this `err:module:import_dll Loading library LIBEAY32.dll (which is needed` – MeNotMe Aug 30 '16 at 00:34
  • IT WORKED ! all i had to do is to run : `/opt/mingw64/bin/x86_64-w64-mingw32-g++ -I/opt/mingw64/include -L/opt/mingw64/lib -o crypto_file_example.exe base64.h en.cpp -lssl -leay32 -std=c++11 -static-libgcc -static-libstdc++ -lm -lgdi32 -lopengl32` after copying libcrypto.a to libeay32.a – MeNotMe Aug 30 '16 at 00:47
  • 1
    @MeNotMe if you've got it working then post an Answer describing what you did and what you think the problem was, in case it is of help to future readers. (don't edit the Question to say "Solved") – M.M Aug 30 '16 at 01:54
  • @M.M Okay .. i'll write full explanation of how i solved the problem – MeNotMe Aug 30 '16 at 03:21

0 Answers0