Saturday, March 8, 2014

Bring a Linux development environment to Windows with MinGW

In general, Linux and Windows development environments aren't compatible. Windows developers often use native integrated development environments (IDE) such as Visual Studio, while Linux programmers use command-line tools such as Make and the GNU Compiler Collection (GCC). While there are cross platform IDEs, most notably Eclipse, the two worlds of Linux and Windows often remain separate.
However, sometimes it's useful to have one development environment that works across Windows and Linux, such as when you're developing software to run on both platforms. Being able to create Make files and shell scripts that run under both operating systems, as well as being able to use the same compiler suite across both platforms, can reduce development time.
The open source MinGW and MSYS projects offer a GNU-based development environment for Microsoft Windows, along with a Bourne shell command-line interpreter. MinGW, which stands for Minimalist GNU for Windows, provides a port of GCC with support for C, C++, Ada, and Fortran, while MSYS provides the Bourne shell as an alternative to Microsoft's cmd.exe. Included in the complete system are well-known Linux development tools such as gcc, make, m4, bison, and flex, along with useful command-line utilities such as grep, gawk, gzip, tar, rsync, and ssh.


To get started with these tools, download the MinGW installer (called min-gw-setup.exe) and run it. You'll have the option to change the destination path from its default of C:\MinGW, along with some options about which menu items are created. Select mingw-developer-toolkit, mingw32-base, mingw32-gcc-g++, and msys-base for a basic installation with a C compiler and shell. You can optionally choose Ada, Objective-C, or Fortran as well.
Extra installation packages are available under All Packages in the left pane. For example, if you want to install the Lua programming language, drill down through All Packages -> MinGW Libraries -> MinGW Supplementary Libraries and mark mingw32-lua for installation.
To start the installation, click on Apply Changes under the Installation menu and then on Apply in the Schedule of Pending Actions window. The installer will download and install the selected packages.
Because MinGW and MSYS are actually two different projects that use a common installer, you must perform some additional steps to link the two environments. First, create the C:\MinGW\msys\1.0\etc\fstab with the following line:
C:\MinGW   /mingw
If you installed MinGW in a different location then alter the path names accordingly. Before you save the file, ensure that there is at least one blank line at the bottom.
As an alternative to manually editing the fstab file, you can start a shell by running C:\MinGW\msys\1.0\msys.bat and then run the command /postinstall/ When you're prompted for the location of the MinGW installation, type c:/mingw.

Compiling a C program

To see how MinGW and MSYS work, create a simple "Hello World" program called hellow.cpp and save it in C:\MinGW\msys\1.0\home\user, where user is your username. If you want to stay completely inside the MinGW environment you can create hellow.cpp using vim:

int main()
    std::cout << "Hello, world!\n";
From within the MinGW Bourne shell compile the program using the GNU C++ compiler (g++):
g++ hellow.cpp -o hellow
The results is the binary executable hellow.exe, which you can run by typing hellow.

Development environment for native Windows programs

Although MinGW brings the GNU compiler suite and some of the familiar Linux development tools to Windows, it doesn't try to provide Linux or POSIX compatibility. You can't write code that makes use of Linux system calls and expect them to work under MinGW. If you need Linux system call compatibility, you can use Cygwin, an alternative to MinGW that provides a POSIX layer for Windows.
Since MinGW provides a native Windows development environment, you can use it to compile programs that call the Win32 API. As an example, create a file called hellowin.cpp:

int main()
        int nResult=MessageBox(NULL,
                        "Hello World",
                        "Message Box",
        return 0;
Compile it using g++ hellowin.cpp -o hellowin and execute it by typing hellowin.

Compiling open source programs

Although MinGW uses the underlying Win32 API, there's no reason you can't use it to compile portable code that can be run on both Windows and Linux. By the judicious use of conditional compilation and cross-platform libraries, you can create code in the MinGW environment that builds and runs under Linux.
Consider, for instance, RHash, a console utility for computing and verifying the hash sums of files. It supports CRC32, MD4, MD5, SHA1, SHA256, SHA512, SHA3, and many other hash functions, and you can compile and run it under both Windows and Linux.
To see how the developers pull off that trick, download rhash-1.3.1-src.tar.gz and copy it into your MinGW home directory. Unpack the tar file with the command tar -zxvf rhash-1.3.1-src.tar.gz. Change directory into the source folder and run make:
cd rhash-1.3.1
You can run the resulting executable, rhash.exe, from the command line. So, for instance, to get the MD5 hash of the project's README file, type rhash -M README.
The shell commands are the same as those used on Linux to build RHash. By examining the code, you can see how the developers use conditional compilation directives such as #ifdef _WIN32 to make the program compatible with the Windows API. For example, in common_func.c, the function rhash_get_ticks() is implemented in two different ways depending on the OS.
If you want to distribute a program compiled under MinGW, you may need to include some of the DLL files from C:\MinGW\bin with your executable. For example, the C++ version of our Hello World program needs at least libstdc++-6.dll and libgcc_s_dw2-1.dll. The libraries are covered by an MIT-style license that grants you the right to use the software without restriction, though you may need to include a copy of the license file with the DLLs. See the MinGW licensing terms for more information.


Common development environments can save time during the software development cycle. MinGW and MSYS provide a way to build, test, and deploy software on Windows using the same tools developers use on Unix-like operating systems.

No comments:

Post a Comment