Developing for Windows on Linux

Build and test both the Linux and Microsoft Windows versions of your project without rebooting. With the free tools MinGW and Wine, you might even call Win32 a cross-platform API.

Replace the command mingw-gcc with whatever the package maintainer called the compiler executable for your package. Presto, you now have a Windows program ready for the world. Or is it?

Debugging with Wine

Wine is the other great boon for developers who need to write programs for the Windows platform. The massive amount of work that has gone into Wine by its developers is phenomenal. This great program allows you to run Windows programs from within Linux. The upshot to this is we now can run our freshly compiled program and see if it actually works as advertised. To do this, use the command wine example1.exe, and you should see the window appear on your desktop (Figure 1). When you set up Wine, you have the options of windows being managed by your window manager, not managed or displayed on their own desktop. This affects how the window looks when you run it. What you see in Figure 1 is an unmanaged application.

Figure 1. The Example Application Running Unmanaged under Wine

If you weren't lucky enough to have typed your program perfectly, you may need to do some debugging to figure out what has gone wrong. Wine can be a great asset here. The option --debugmsg [debugchannel] outputs the results from one or more debug channels within Wine. Examples of the available debug channels are:

  • relay: writes a log message every time a Win32 function is called.

  • win: tracks Windows messages.

  • all: tracks all messages.

Don't use all unless you really need it. The amount of output quickly can overwhelm even the most detail-obsessed programmer. A complete list of available debug channels can be found on the Wine site.

Compiling a Native Version for Linux

We now have a wonderful, working, bug-free program that runs under Windows. Considering that all of the work was done under Linux, wouldn't it be nice if we also could have our program run under Linux? The good folks at the Wine Project have come to the rescue again. Part of the project includes winelib, a library that provides the interface to Linux for your Windows source code. In order to use this functionality, you need to install the wine-devel package for your distribution. If you installed from source, the required files already should be available.

Included in the wine-devel package is a Perl script called winemaker. This script is designed to go through your source files and directories and make the required changes to get it to compile correctly against winelib on Linux. Things it checks include filename case and line ending characters. In addition, it replaces file path back slashes with forward slashes and does many other things. By default, it backs up any source files it needs to change. It converts your project to winelib, making all kinds of automagic changes. To compile, you simply run:

winemaker .
./configure --with-wine=/path/to/wine
make

to create a Linux executable. The dot you see above is there on purpose. You hand in the path where winemaker can find the source files it needs to analyze; here, the files are in the current directory.

In our case, our sample doesn't have any project files, and winemaker thinks this is a bit of a problem. We can do the steps involved simply by hand. Instead of executing mingw-gcc to compile our program, we use winegcc with the exact same command-line parameters. This creates a file ending in .so and a shell script to handle the program execution. We now have our Windows source code compiled and running under Linux.

Conclusion

I hope I've been able to convince some of the Windows developers out there that they can work effectively from within Linux. With GCC allowing compilation of an executable for Windows, and Wine providing great support in running and debugging, there is no real reason to boot up Windows in most cases. The only reason would be if your favourite IDE didn't run correctly under Wine, but then you always could volunteer to fix that problem, right?

As this was only a short introduction, I did not cover support for MFC or the creation of DLLs. Both of these topics are discussed in more detail at WineHQ and the MinGW site.

Resources for this article: /article/7555.

Joey Bernard is a systems architect for CARIS, a GIS company in Canada. He's never actually done any GIS work, mostly just Oracle, UNIX systems programming and some Windows programming.

______________________

Joey Bernard has a background in both physics and computer science. This serves him well in his day job as a computational research consultant at the University of New Brunswick. He also teaches computational physics and parallel programming.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

More recent howto

Mitch Thomson's picture

The rather new MinGW-w64 project allows for creation of native 32- and 64-bit executables.
See compile Windows binaries under Linux for a quick howto.

Door number one, Door number two... Door number 36...

Confused's picture

I've used MinGW and MSYS before <microsoft bashing> my store-bought copy of vista suddenly decided it wasn't a valid licensed copy (which, to be fair, was resolved quickly and with lukewarm courtesy by tech support, and my copy did decide it was licensed after all, and I did not give it a fair chance to screw up again), and I decided it was a good time to wash my hands of the OS forever and stopped putzing around with cygwin in order to do my job without going into debt </microsoft bashing>

That said, though, the number of programs to choose from on the MinGW website is dizzying. Which program, specifically, did you grab and compile on your machine? Did you grab one of the .exe files and install it through wine, or is there a source distribution that I can build using the cozy make, su, make install procedure without invoking wine?

Just trying to navigate my way through a maze of identical-looking binaries, and I'm hoping that asking someone who actually got something working can shed a little bit of light on things for me.

Re: Developing for Windows on Linux

Anonymous's picture

(Replied to the wrong message earlier)

After reading this article, I immediately tried to apply it by compiling a couple of simple programs. My goal is to build Windows binaries and I want to include the Linux machines in the build process as well (using distcc). I built a cross compiler from the sources and everything seemed to build successfully. (The information for this came from this link: http://ranjitmathew.tripod.com/phartz/gcj/bldgcj.html

As a first test, I tried to compile a simple "Hello" file (hello.c) using the newly-created mingw32-gcc executable:

#include
int main(void) {
printf("Hello
");
return 0;
}

This worked and I was able to link hello.o on a Windows computer. Next, I tried to compile the C++ version (hello.cpp):

#include
using namespace std;
int main(void) {
cout
But this gave many errors. I knew it was that the include files were not being found, so I manually added them to the command line. I simply included all of the paths that the Linux g++ was using. (I discovered those paths by using the -v switch when compiling.) I even tried different variations with the include files that came with the mingw32 sources. I've been able to remove most of the errors, but a few still remain. The first one is this:

/usr/include/g++/i486-suse-linux/bits/c++locale.h:59: error: syntax error

which is complaining about this typedef:

typedef __locale_t __c_locale;

apparently, __locale_t is not defined anywhere (as far as I could tell.) Incidently, the same .h file under Windows looks like this:

typedef int* __c_locale;

I'm not an expert with Linux or g++, but I'm guessing there's some configuration issues as to why the new mingw32-g++ can't find the right header files. Even moving the mingw32-* binaries into /usr/bin doesn't help. Am I missing some header files or something else?

--Dig

Re: Developing for Windows on Linux

Anonymous's picture

Well, I missed that the angle brackets ( C++ insertion operator) confused the HTML reader (even after preview...). This is the HTML-friendly C++ code:

#include [iostream]
using namespace std;
int main(void) {
cout [[ "Hello" [[ endl;
return 0;
}

Replace [ and ] with less-than and greater-than.

--Dig

Re: Developing for Windows on Linux

Anonymous's picture

The first part of the section Win32 programming is simply dead wrong (well, almost). No proofreading?

Quote1: "All processes are considered threads by the operating system."

- No, processes are not considered threads. A process is a set of resources (private virtual address space, file handles, etc, etc). Each process has at least one thread though - the main thread of execution.

Quote2: "This makes the process context slightly lighter than the traditional heavyweight process model used in UNIX-like operating systems."

- The process context is not any lighter than UNIX - at least not because of the non-existing "everything-is-a-thread model".

Quote3: "With the correct permissions and the correct address, one program could twiddle another program's bits."

- No. Nope. Simply not possible without writing a kernel-mode driver. Unless one of the program exposed its data through named memory-mapped files (similar to mmap) with incorrect permissions set.

Articles like this only help to spread the many misconceptions on the NT family of operating systems. I seriously hope you haven't written any commercial applications for Windows ;-)

Whenever you have time, buy and read the latest edition of "Inside Windows NT". You won't regret it.

// Dev Anonymous

Re: Developing for Windows on Linux

joeybernard's picture

I do stand corrected on these issues. It was a combination of a lame attempt at humour/Microsoft bashing, combined with my working with mythology rather than going to sources and checking what I assumed was "common knowledge". While I did not have the money or inclination to get the book anonymous suggested, I did check out the great information on the sysinternals site (http://www.sysinternals.com). I claim temporary stupidity, simply because I never had to deal with systems level programming in Windows. Please accept my apologies.

Re: Developing for Windows on Linux

Anonymous's picture

After reading this article, I immediately tried to apply it by compiling a couple of simple programs. My goal is to build Windows binaries and I want to include the Linux machines in the build process as well (using distcc). I built a cross compiler from the sources and everything seemed to build successfully. (The information for this came from this link: http://ranjitmathew.tripod.com/phartz/gcj/bldgcj.htm)

As a first test, I tried to compile a simple "Hello" file (hello.c) using the newly-created mingw32-gcc executable:

#include
int main(void) {
printf("Hello
");
return 0;
}

This worked and I was able to link hello.o on a Windows computer. Next, I tried to compile the C++ version (hello.cpp):

#include
using namespace std;
int main(void) {
cout
But this gave many errors. I knew it was that the include files were not being found, so I manually added them to the command line. I simply included all of the paths that the Linux g++ was using. (I discovered those paths by using the -v switch when compiling.) I even tried different variations with the include files that came with the mingw32 sources. I've been able to remove most of the errors, but a few still remain. The first one is this:

/usr/include/g++/i486-suse-linux/bits/c++locale.h:59: error: syntax error

which is complaining about this typedef:

typedef __locale_t __c_locale;

apparently, __locale_t is not defined anywhere (as far as I could tell.) Incidently, the same .h file under Windows looks like this:

typedef int* __c_locale;

I'm not an expert with Linux or g++, but I'm guessing there's some configuration issues as to why the new mingw32-g++ can't find the right header files. Even moving the mingw32-* binaries into /usr/bin doesn't help. Am I missing some header files or something else?

-- Dig

Re: Developing for Windows on Linux (Ignore previous post)

Anonymous's picture

Ignore the above, I replied to the wrong message. I've reposted it. D'oh!

--Dig

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState