libressl / portable

LibreSSL Portable itself. This includes the build scaffold and compatibility layer that builds portable LibreSSL from the OpenBSD source code. Pull requests or patches sent to tech@openbsd.org are welcome.
https://www.libressl.org
1.35k stars 269 forks source link

Crosscompile linux to windows: linking fails, undefined reference SSL_library_init 3.9.2 #1053

Open tillpp opened 4 months ago

tillpp commented 4 months ago

I compiled it for windows using linux with the README.windows commands i got three library files, tls.a,ssl.a and crypto.a but when i tried using them i got the following error message:

/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/_.dir/objects.a(main.cpp.obj):_/src/main.cpp:5: undefined reference to `SSL_load_error_strings'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/_.dir/objects.a(main.cpp.obj): in function `main':
_/src/main.cpp:6: undefined reference to `SSL_library_init'

but everything works well when compiling with just gcc on linux for linux, the error only occurs when crosscompiling

busterb commented 4 months ago

Can you please say more about what commands you used to build your main.cpp file, and its contents?

busterb commented 4 months ago

Here's a quick working example for you to play with:

$ make install DESTDIR=`pwd`/install > /dev/null
libtool: warning: remember to run 'libtool --finish /usr/local/lib'
libtool: warning: remember to run 'libtool --finish /usr/local/lib'
libtool: warning: remember to run 'libtool --finish /usr/local/lib'
$ cat main.cpp 
#include <openssl/ssl.h>

int main(int argc, char const *argv[])
{
    printf("SSL_library_init returned %d\n", SSL_library_init());
}
$ x86_64-w64-mingw32-c++ main.cpp -Iinstall/usr/local/include -Linstall/usr/local/lib -lssl -lcrypto -lws2_32 -lbcrypt -static
$ wine ./a.exe
SSL_library_init returned 1

Keep in mind you should install the library somewhere (implied, but not explicitly spelled out in the README) in order to use it, and you'll also need to explicitly link in some support libraries on Windows that are implicit on other platforms. I think the linker script should help you automatically here, but if you're cross-compiling straight from the command line, you're probably not using it.

Hope this helps!

busterb commented 4 months ago

@RandomInEqualities do you think this needs more clarification in the docs, or is something like this fairly clear when folks are targeting Windows already?

RandomInEqualities commented 3 months ago

I think if using CMake this is all handled by that, so nothing is needed there! I'm not sure about cross compiling, since that does require the extra -lws2_32 and installing the library. Maybe it could be mentioned in an additional section about cross-compiling (but mind I don't cross compile myself, so I don't know too much about it).