Home > Articles > Programming > C/C++

Cross-Compile Your C++ Apps with XMingwin

  • Print
  • + Share This
This useful open-source library can help C++ developers port their applications from Linux to Windows -- without having to do any extra work.
Like this article? We recommend

Suppose you're a Linux-based developer. You wrote a complex library that calculates weather statistics, or creates sound effects, or de-blurs photographic images. You successfully deliver programs to an extensive list of customers or co-workers. So far, so good. Yet, users have been asking you for a version of your software that run on Windows.

You're flattered and you welcome the attention, but it's a challenge. Sure, your code is well-written and it will port easily enough, but how can you take on the burdens of managing a whole new development environment, such as Microsoft's Visual C++ or Borland's C++ Builder? Will you have to abandon your usual working environment for the inconveniences of dual-booting, or use a second host that needs tending?

Well, no. You don't have to relocate. You can stay at home on your existing Linux desktop, and tweak your environment a bit so that it simultaneously generates Linux- and Windows-targeted results. XMingwin, an open source project, can do help you do that. It produces native Windows programs that do not rely on any third-party DLLs.

A Useful Instance of A Big Topic

XMingwin is a "cross-development" package. The "cross-minimalist-GNU-for-Windows" project (occasionally spelled "Xmingw," "Xmingw32," and so on) is actually only a tiny selection from a far bigger story. The open-source technologies involved are flexible enough to have the potential to work with any host, any target, and any language. In principle, you could write in Ada on an old MS-DOS box, and deliver code that works on mainframes.

Understanding the full generality of cross-development is rather breathtaking, if not intimidating. Rather than tackle all its abstractions, let's look at a specific, concrete model case. Generalize your success with this straightforward example to your own situation.

Our model program will include two C++ source files, p1.cc and p2.cc:

 # This is the source of p1.cc.
 #include <iostream>
 #include <stdio.h>

 float this_sum();

 using namespace std;

 int main(int argc, char *argv[])
   cout << "The result is " << this_sum() << ".\n";
   return 0;

 # This is the source of p2.cc.
 float this_sum()
   return 7.3 + 16;

Generate an executable with these under Unix by invoking

 g++ -c p1.cc
 g++ -c p2.cc
 g++ -o p p1.o p2.o

Launch the generated application,


and you'll see

 The result is 23.3.

In practice, you probably use a Makefile or another more advanced generation facility. (These, also, are abstractions outside the scope of this introduction.) Once you have a simple case working reliably, you should be able to apply the principles to Makefile on your own.

Next, install an XMingwin set of binaries (such as the one found here). With many open-source projects, it's most convenient to start from sources and generate results yourself. Although you'll eventually want to do that for XMingwin, I don't advise you to start there. Compiler generation--especially of associated libraries—is complex enough. Leave it to others for your first experiments.

  • + Share This
  • 🔖 Save To Your Account