开发者

Cross-compiling on Windows and Linux

I am developing an application that will have to run on both Windows and Linux.

So far I have it running on Linux using GCC 4.4.1 using my Makefile.

However, I also need to compile on Windows. The source code will compile on Windows a开发者_如何学Pythons I have #defined all the areas of the code that separate the different compilers. I.e.:

#ifdefined (LINUX)
/* Do linux stuff */
#else
/* Do windows stuff */
#endif

And so far the code is very simple as I am just starting this program. Basically I just want to test my idea.

However, is it as simple as compiling the source code on Linux?

Then when I want to compile on to Windows I could copy the files to the Windows platform. Then opening the source code files in Visual Studio C++ and creating a project and then compiling to create my binary?


Take a look at CMake as well.


Using Visual C++ is one way of building your C project on Windows. It's not the only way, though.

VC has project files that you will need to create. But then realize that you'll have to maintain the build configuration in two different places (make & .vcproj).

There are other options as well:

  • You can also use make on Windows (e.g. on Cygwin)
  • There's nmake, msbuild, etc.
  • You could also use ant (which will work on both platforms).

If I were you I'd try to use as few different build tools as possible.


SCons is an excellent cross-platform build system. Instead of writing a Makefile, you write a SConstruct file, which is usually simpler and yet more powerful. The scons command will (by default) build for the operating system on which it's run, providing an easy way to manage cross-platform builds.


For simple console applications (and even more complex ones, like the Qt framework) you can build on Windows using MinGW. Building with MinGW is pretty similar to building on Linux, and it isn't unlikely that you can use the same Makefile with no or only minor modifications. This has the major advantage that you don't need to maintain two sets of Makefiles / project files.

You can also use Cygwin, which basically is an emulation layer of the Linux API for the substantial functionality. Using Cygwin however has the disadvantage that it links to its own library cygwin1.dll. This might not be an issue for you as you can simply ship this DLL with your program. You can also suppress using this by using the option -mno-cygwin as compiler option for gcc / g++. However, for some functionality the cygwin1.dll functionality is required.

I can recommend MinGW for building programs on Windows that have originally been written for Linux. I'm using this for various command line programs, and for GUI applications I'm using the Qt framework with the MinGW compiler. If you want to build GUI applications you should definitely use a platform independent GUI framework as it makes things much easier.


You may be able to use MinGW to cross-compile Windows binaries directly from Linux. Most of the popular Linux distros now have MinGW packages available that make this very straightforward.

Then you can build all your Windows and Linux binaries in one go, on one machine, from one Makefile. No need to mess around trying to get GNU Make to work properly in Windows, let alone worry about Visual Studio.


CMake can help you keep your build portable across several toolchains. However, you still have to deal with the incompatibilities between those at the language and library level.

Which is why I recommend using GCC, the autotools, etc on all platforms. On windows, the GNU toolchain is called MinGW/MSys. Look at MSysGit for an example.


Yes. It's that simple. Just keep in mind the whole time that you need to enclose all OS-specific stuff in #ifdefs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜