0

I can already cross-compile some simple applications using x86_64-w64-mingw32-g++. However, I cannot find out how to find libraries for my project (in my case I need to use SDL2 and SDL2_image). I know how to install these libraries already (sudo apt install libsdl2-dev libsdl2-image-dev), but apt search does not show any libraries for x86_64-w640mingw32-g++.

Here are the links I've looked at:

https://arrayfire.com/cross-compile-to-windows-from-linux/

https://stackoverflow.com/questions/2033997/how-to-compile-for-windows-on-linux-with-gcc-g

Is there a way to use precompiled libraries, or do I need to add the source code in my project?

Update: I followed the instructions advised by Laurent Jospin, and can compile a "Hello World" program that works with Windows (I sent it to a friend to test). However, I cannot compile my program that uses SDL.

Terminal output:

$ /opt/mxe/usr/bin/x86_64-w64-mingw32.shared-g++ -O3 main.o Game.o TextureManager2D.o Map.o Entity.o Player.o -mwindows -lSDL2 -lSDL2_image -o ../../../endeavour-client.exe
/opt/mxe/usr/lib/gcc/x86_64-w64-mingw32.shared/5.5.0/../../../../x86_64-w64-mingw32.shared/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function `main':
/opt/mxe/tmp-gcc-x86_64-w64-mingw32.shared/gcc-5.5.0.build_.crt/../gcc-5.5.0.build_/mingw-w64-v8.0.0/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2: error: ld returned 1 exit status
$

Changing -mwindows to -municode, -d UNICODE, -Wl,-subsystem,windows, or removing it all do not work.

Thuong Vo
  • 77
  • 11
  • It could be faster and simpler to buy a cheap hard disk with windows on it. – Basile Starynkevitch Apr 22 '21 at 05:03
  • It would, but I don't have the means or money to buy one. – Thuong Vo Apr 22 '21 at 05:24
  • Then forget about Windows and keep coding on Linux. BTW, I am coding since 1974 and never used Windows. If you cannot (and so do I) afford Windows, don't use it. If your boss or client needs Windows, he has to pay you a Windows license. Using some unpaid Windows is illegal in most countries (so you can get legal trouble or some fine). – Basile Starynkevitch Apr 22 '21 at 05:25
  • BTW, you could be interested by the [RefPerSys](http://refpersys.org/) project (symbolic AI on Linux, free software). Then contact me by email to `basile@starynkevitch.net` – Basile Starynkevitch Apr 22 '21 at 05:32
  • Ask your friend to compile the Windows variant on his/her own Windows computer from your C++ code, and send him some C++ code that is working on Linux – Basile Starynkevitch Apr 23 '21 at 06:43

2 Answers2

1

What you are looking for is the M Cross Environnement: https://mxe.cc/

It is a set of makefiles able to download and cross compile a selection of popular libraries for windows on linux. By default it builds static libraries, such that you ends up with .a libraries that get merged into the final .exe, meaning you don't have to worry about shipping the dlls with your app. But if you prefer a modular structure, it can also builds some dlls.

The list of libraries they do provide is quite impressive. If a library is missing on the other hand you can still install it by copying the corresponding header files and dlls. In some specific situation you might have to cross-compile one of your dependency (I had to do that for an app using ruby scripting. The official windows build of ruby is somehow incompatible with certain libraries built with mingw. But this is rather exceptional).

Laurent Jospin
  • 604
  • 5
  • 9
0

The correct command is:

$ /opt/mxe/usr/bin/x86_64-w64-mingw32.shared-g++ -O3 main.o Game.o TextureManager2D.o Map.o Entity.o Player.o -mwindows -lmingw32 -lSDl2main -lSDL2 -lSDL2_image -o ../../../endeavour-client.exe

Edit: Note that having -lmingw32 -lSDL2main -lSDL2 -lSDL2_image in that exact order is vital.

I, being the absolute idiot that I am, forgot to include the -lmingw32 AND -lSDL2main flags in the command, which is why there is the issue with WinMain not being found. When using MinGW-w64 as the cross compiler, the -lmingw32 library has to be included. And of course, SDL requires the -lSDl2main flag when building for windows (but interestingly enough, not on Linux). Here are the links I used. (I had an uncommon insight in replacing -lSDLmain with -lSDL2main, as the links are pretty old.)

MinGW Starter Guide

Code::Blocks Forums

SO: Undefined Reference to WinMain

Thuong Vo
  • 77
  • 11