First of all, why do this?
1. developers without access to windows can now build, test and distribute binaries for windows.
2. In various places the source code is poor c++ and compiling with gcc finds these cases (in the windows specific code) which improves code quality and portability.
3. gcc is free
software. You may fix problems you find in it. Visual Studio is not free, and has many bugs which have wasted the time of people testing my plugins, and therefore also my time.
4. As a gcc contributor, I am a bit biased, however gcc is capable of generating more efficient code, reporting more understandable warning and error messages, and has a superior debugger (gdb)
5.
OpenCPN is free
software, as are all of the dependencies, therefore it only makes sense that we would like to also use a free compiler if at all possible.
If you are using
linux, you may skip ahead to the cross compiling section below as it is much simpler and quicker.
To start off, getting this working was a fairly painful process. A
single change in any build file requires several hours to see the results (because invocations are very slow via wine and the build system uses a lot of these) so it took me a few days to achieve this. However, once
opencpn.exe is running in wine, the performance is the same as if it were running natively in
linux.
First install wine so we can run windows binaries (only if you are not using windows)
$ sudo apt-get install wine
Next, download msys/mingw installer from mingw.org and install gcc and g++ as well as gettext.
Run msys (wine cmd; cd c:/mingw/msys/1.0; msys) and you should have a window with a shell.
fix /usr thing, (hack in include directories for now)
$ ln -s / /usr
Now download and extract wxwidgets (I used version 2.8.12)
edit src/msw/treectrl.cpp to have following change:
+++ treectrl.cpp 2014-02-28 11:27:59.367810324 +0800
@@ -43,6 +43,10 @@
#include "wx/msw/dragimag.h"
?#include "wx/msw/uxtheme.h"
+#ifdef __MINGW32__
+#define TV_DISPINFO NMTVDISPINFO
+#endif
+
// macros to hide the cast ugliness
// --------------------------------
Now, from the msys shell build wxwidgets:
$ cd wxWidgets*
$ mkdir build-mingw32
$ cd build-mingw32
$ ../configure --with-opengl --enable-unicode
$ make
$ make install
Note: If make fails with make errors (not compilation error) run make again and it may succeed.
Note: you may also attempt with --enable-graphics_ctx but I have not had the chance yet
Next, must compile cmake from source, and install it.
(the cmake binary compiled with VS may
work under microsoft's windows, but crashes under wine)
I don't have detailed instructions but it is straightforward.
To avoid many issues specific to compiling OpenCPN using mingw, you must use the branch from my fork of opencpn for various changes required to compile using mingw
From a shell (with git installed), or with a git program:
$ git clone -b mingw
https://github.com/seandepagnier/OpenCPN.git
From msys shell:
$ cd OpenCPN
$ mkdir build-mingw32
$ cd build-mingw32
$ cmake -G "MSYS Makefiles" ..
$ make
$ make install
note: it may be possible to use mingw32-make instead of make in msys, but I have not tried.
Now opencpn.exe is installed in program files, but cannot run until you copy the wxwidgets dll files into the same directory (we can later make this automatic as it is already for visual studio)
I have successfully built dashboard_pi as proof that plugins are working, but renders some of the
instruments poorly. I believe wxwidgets must be configured --with-graphics_ctx to fix this. I didn't try yet because of possible
license issues with gdiplus.h which is not included with mingw. Please advise.
The grib
plugin does not build because there is no zlib.. maybe someone can help me out here. On windows, does it need to use the zlib in wxwidgets? Does this mean when building wxwidgets I must configure --with-zlib to solve this?
An important caveat: If you change the cmake files, you must rm -rf the build directory, as not doing this causes cmake to generate a different makefile which then fails to link. I think this is probably a bug in cmake.
You can reduce the file sizes of the exe as well as the dll files dramatically by using the strip command. The result is the files are the same size as the visual studio versions.
Unfortunately the visual studio dll files are also needed for the ocpn_gltest1.exe program which I cannot find the source (to
rebuild with mingw instead) or else opengl is disabled.
------------------------------------------------
Cross compiling OpenCPN for windows under linux.
------------------------------------------------
This is much more convenient and the build is much faster. The resulting binary can be tested in wine.
Install mingw cross compiler, and wine for testing:
$ apt-get install mingw32 wine
cross compile wxwidgets:
$ ../configure --prefix=/usr/local/i586-mingw32 --host=i586-mingw32msvc --with-opengl --enable-unicode
$ make
$ sudo make install
note: you may attempt --with-graphics_ctx as well
In /usr/share/cmake-2.8/Modules/FindwxWidgets.cmake
There is a bug in the cmake module FindwxWidgets.cmake that prevents any library using wxWidgets to cross compile for Windows from Linux. Please note that this bug is different from issue 0006187. Lines 192 to 198 are concerned by the present bug:
IF(WIN32 AND NOT CYGWIN AND NOT MSYS)
SET(wxWidgets_FIND_STYLE "win32")
ELSE(WIN32 AND NOT CYGWIN AND NOT MSYS)
IF(UNIX OR MSYS)
SET(wxWidgets_FIND_STYLE "unix")
ENDIF(UNIX OR MSYS)
ENDIF(WIN32 AND NOT CYGWIN AND NOT MSYS)
The problem is that, when cross compiling from Linux for Windows, the
test "WIN32 AND NOT CYGWIN AND NOT MSYS" is true but find style should
be "unix", because the Windows-based wxWidget library is installed in a
unix
environment.
Therefore, the following line replacement corrects this bug:
IF(WIN32 AND NOT CYGWIN AND NOT MSYS AND NOT CMAKE_CROSSCOMPILING)
SET(wxWidgets_FIND_STYLE "win32")
ELSE(WIN32 AND NOT CYGWIN AND NOT MSYS AND NOT CMAKE_CROSSCOMPILING)
IF(UNIX OR MSYS OR CMAKE_CROSSCOMPILING)
SET(wxWidgets_FIND_STYLE "unix")
ENDIF(UNIX OR MSYS OR CMAKE_CROSSCOMPILING)
ENDIF(WIN32 AND NOT CYGWIN AND NOT MSYS AND NOT CMAKE_CROSSCOMPILING)
After making this change, cross compile opencpn for windows.
$ git clone -b mingw
https://github.com/seandepagnier/OpenCPN.git
$ cd OpenCPN
$ mkdir build-mingw32-cross
$ cd build-mingw32-cross
$ cmake -DCMAKE_TOOLCHAIN_FILE=../Toolchain-mingw32.cmake ..
$ make
Currently make install, and make package are dysfunctionalgl