Cross compiling on Linux/FreeBSD for a Windows target with MinGW32

The material below is somewhat old, and there are more recent postings available including:

I haven't tested those, so I am leaving the material below intact.

I wanted to compile bog-standard Fortran 95 code on a Linux (Fedora 11) system for subsequent execution on a Windows computer. This is amazingly easy to do with the MinGW cross compiler suite if one knows how, but the documentation is fairly vague on the basics of cross compilation, hence this posting.

Admittedly, cross-compiling a threaded interactive GUI based program (based on Unix libraries not intended for porting to Windows) is substantially more complex, and I haven't done that. But I think just compiling a conforming program in a standardized language is still useful and interesting for some users, and deserves appropriate documentation.

I believe the same procedure would be effective for any of the languages in the GNU compiler suite for any recent rpm based distribution, but I have only worked with Fortran. At the end of the page I report the procedures for Debian-derived and FreeBSD systems.

Step 1. Installing the cross compilers on YUM/RPM based distributions

You can install just the Fortran, gcc and c++ cross-compilers:
# yum install mingw32-gcc-fortran
[As of June 2022 this doesn't work, and I am unaware of an alternative.] or the entire suite including Objective-C, Java and numerous Unix-C specific libraries and development tools.
# yum install mingw32\* 

but the later is almost 400 megabytes against less than 100 megabyes for the subset, and not much of the later is relevant for fortran.

Step 2. Compiling the fortran program

The MinGW compilers are the same as the compilers in the GNU compiler suite, except that they have "i686-pc-mingw32-" prepended to the command name and they link to Windows compatible libraries. The prepend string is defined by the particular Linux distribution and this particular string value is RedHat-specific and subject to change. If your computer doesn't respond to that name, you can probably locate the command names in /usr/bin or nearby. But at this time with Fedora 11 one can compile the f77 program "hello.for" with:

%> i686-pc-mingw32-gfortran -o hello.exe -static hello.for

The options are passed to the GNU compiler, any you require on Linux you will likely want to keep on Windows where they should have the same effect. The -static option is a simple way to make calls to DLLs resolve in Windows or Wine. Alternatively you could copy the MingGW DLLs to an appropriate directory, or (in the case of Wine) follow the instructions here. But I haven't tried either of those, since -static is so much easier for me and my user. If you omit the -o argument, you get "a.exe" as the name of the Windows executable.

Step 3. Testing the program

It is nice to test the program without moving it to another system. If you haven't installed wine:
# yum install wine -y
and run the test with:
%> wine hello.exe

With the default wine configuration you need the explicit ".exe" or you get an "unsupported architecture" error message. It isn't necessary in a real windows system.

System dependencies

I found no unexpected system dependencies compiling my often ported (but 21,000 lines) program. As desired:

I have not tested portability of unformatted or direct access files.

Other Unix Distributions

FreeBSD

I used the FreeBSD 7.2 CD-ROM dated May 2009.

su
cd /usr/ports/devel/mingw32-gcc
make NO_CHECKSUM=YES
ming32-gfortran -static -o hello.exe hello.for
cd /usr/ports/emulators/wine
make
/usr/local/bin/mingw32-gfortran hello.f
wine hello.exe

The NO_CHECKSUM option may not be required if your ports tree has the correct distinfo file. As of June 2022 (FreeBSD 13.1) I had to add "MAKE_JOBS_UNSAFE=yes". There are 5 other pieces to the MinGW32 system in the devel directory, all begin "mingw32".

Ubuntu

I used the Ubuntu 9.04 CD-ROM and started with:

sudo apt-get inwstall mingw32\*
but that package did not include the fortran frontend. A bit of searching around the net turned up the somewhat older "Speedblue" package:
sudo apt-get install mingw32-g77-speedblue
/usr/i586-mingw32/bin/i586-mingw32-g77 -static -o hello.exe hello.for
wine hello.exe

Surprisingly, the Speedblue executables are placed out of the standard path. The g77 compiler is an older fortran 77 compilter that has few of the fortran 95 features that are included in gfortran, but is still of high quality if you have conforming code.

Other references

http://pld.cs.luc.edu/courses/271/mnotes/mingw.html

Comments welcome.

Daniel Feenberg
19 June 2022
feenberg@nber.org