3

I have some RSA code, however, I can't get it to compile & link properly.

build.sh:
x86_64-w64-mingw32-g++ rsa.cpp -L/usr/local/opt/openssl/lib -lcrypto -lssl -I/usr/local/opt/openssl/include/ -static-libstdc++ -static-libgcc -o main.exe 


result:
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0xe6): undefined reference to `BIO_new_mem_buf'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0xef): undefined reference to `BIO_f_base64'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0xf7): undefined reference to `BIO_new'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x10b): undefined reference to `BIO_push'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x130): undefined reference to `BIO_read'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x146): undefined reference to `BIO_free_all'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x177): undefined reference to `EVP_PKEY_new'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x193): undefined reference to `EVP_PKEY_assign'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x198): undefined reference to `EVP_MD_CTX_create'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x1a5): undefined reference to `EVP_sha256'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x1c7): undefined reference to `EVP_DigestVerifyInit'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x1f1): undefined reference to `EVP_DigestUpdate'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x218): undefined reference to `EVP_DigestVerifyFinal'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x234): undefined reference to `EVP_MD_CTX_cleanup'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x254): undefined reference to `EVP_MD_CTX_cleanup'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x26e): undefined reference to `EVP_MD_CTX_cleanup'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x2ac): undefined reference to `BIO_new_mem_buf'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x2da): undefined reference to `PEM_read_bio_RSA_PUBKEY'
collect2: error: ld returned 1 exit status

both libcrypto.a and libssl.a exist in /usr/local/opt/openssl/lib. I'm building on OS X mingw compiler a windows application.

EDIT: objdump -d /usr/local/opt/openssl/lib/libcrypto.a > ~/libcrypto.a.txt

enter image description here

EDIT2: Tried grouping: x86_64-w64-mingw32-g++ rsa.cpp -L/usr/local/opt/openssl/lib -Wl,--start-group -lcrypto -lssl -Wl,--end-group -lws2_32 -lwsock32 -liphlpapi -I/usr/local/opt/openssl/include/ -static-libstdc++ -static-libgcc -o main.exe, same result

Gintas_
  • 4,940
  • 12
  • 44
  • 87
  • 1
    I had those errors at first when my code needed openSSL 1.0.2 and i had the library for 1.1.0. – hugo411 May 09 '18 at 18:03
  • 2
    Are those `.a` libs created with the same compiler & configuration as your app? Can you rebuild them? – Ripi2 May 09 '18 at 18:03
  • 1
    why don't you check if these symbols are actually exported by libraries in question using related tools (objdump/etc)? – C.M. May 09 '18 at 18:50
  • 1
    also, order of libraries mentioned on cmdline is important -- try things like mentioning `-lcrypto` second time after `-lssl` or [grouping](https://stackoverflow.com/questions/5651869/gcc-what-are-the-start-group-and-end-group-command-line-options?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – C.M. May 09 '18 at 18:53
  • 1
    @C.M. check my edit – Gintas_ May 09 '18 at 18:58
  • 1
    try my second suggestion... I bet that symbol is referenced by libssl. Changing the order of these libs may work – C.M. May 09 '18 at 19:00
  • 1
    @C.M. tried grouping; tried mentioning -lcrypto after -lssl, still same thing! – Gintas_ May 09 '18 at 19:04
  • 1
    @Ripi2, no, those libraries come with openssl, which I got by `brew install openssl` and `brew upgrade openssl` – Gintas_ May 09 '18 at 19:06
  • 1
    weird... try: moving both of them to the end of cmdline, group them using `--start-grouping`/etc, make sure you are actually using libraries you think you are using (e.g. use `-l:libssl.a` instead of `-lssl`) – C.M. May 09 '18 at 19:08
  • 1
    one more thing: rsa.cpp references `EVP_DigestVerifyInit`, but libcrypto.a exports `_EVP_DigestVerifyInit` (note leading underscore). Maybe you forgot some preprocessor definition that changes names of functions you are referencing? – C.M. May 09 '18 at 19:10
  • 1
    OSX and Windows are very different targets. I don't think libs obtained with brew can be compatible with Windows. I'd try to compile them on my own, likely with some changes to makefile. – Ripi2 May 09 '18 at 19:11
  • 1
    @C.M. moving it to the end after `-o main.exe` doesn't help. Not familiar with assembler, but I believe it should have been included in openssl headers if it was neccessary. @Ripi2 interesting point, this makes a lot of sense. But then how do I fix it. – Gintas_ May 09 '18 at 20:02
  • 1
    Check [this](https://stackoverflow.com/questions/40917002/visual-studio-builds-openssl-with-underscore-prefix-to-symbols) out. Looks like calling conventions change exported symbol name. I bet your rsa.cpp observes these functions as __stdcall symbols, while your libraries were built with __cdecl (either explicitly or by default). Or something to that effect... You are likely missing some preprocessor definition in rsa.cpp. Easy way to check -- explicitly declare EVP_PKEY_assign function with various call conventions, use it and see which one will cause linker to stop complaining. – C.M. May 10 '18 at 02:46
  • 1
    ... and yes, if your libraries are compiled for another target -- it probably won't work. – C.M. May 10 '18 at 02:52
  • 1
    @C.M. could you let me know specifically what should I do to fix it? – Gintas_ May 12 '18 at 12:42
  • 1
    Not really, I'd check symbols exported by a library (`nm -gC `) and compare with symbols linker looks for and try to figure out why they differ and what they should be. In fact, just yesterday I dealt with very similar problem -- one lib exported `std::string` as `std::__cxx11::basic_string`, another as `std::string`. Both libs compiled by the same compiler for the same target, the only difference is that one had `-std=c++11` -- this made it unusable for C++03 application. – C.M. May 12 '18 at 20:12
  • 1
    I ended up using windows 10 virtual machine to compile openssl. It worked, but required some .dll (which I don't want to add), so I ended up using crypto++. Thanks anyway – Gintas_ May 12 '18 at 20:32
  • 1
    no probs. Btw, root of my problem was that one lib was compiled under `sudo` -- and ended up picking up older gcc (I have newer version of gcc compiled and installed under /usr/local -- for some reason root user didn't pick it up as default one). So, making sure every build used same compiler fixed everything and removed need of tinkering with compiler flags. – C.M. May 12 '18 at 21:17
  • 2
    Why don't you simply build your code on Windows with MinGW ? Compile and check it for Mac native with XCode, if it is working - build it for Windows, this is not a big deal to use VirtualBox or another virtual machine on Mac. – Victor Gubin May 15 '18 at 17:03
  • @Gintas_ Did you try to set `DYLD_FALLBACK_LIBRARY_PATH` Like `DYLD_FALLBACK_LIBRARY_PATH=/usr/local/opt/openssl/lib:$DYLD_FALLBACK_LIBRARY_PATH` ? – Aditya Pawaskar May 16 '18 at 05:14

0 Answers0