Cross compiling on Windows and Linux

I am developing an application that should run on both Windows and Linux.

So far this works for me on Linux using GCC 4.4.1 using my Makefile.

However, I also need to compile Windows. The source code will compile on Windows, since I #defined all areas of code shared by various compilers. I.e:.

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

And while the code is very simple, since I'm just starting this program. Basically, I just want to test my idea.

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

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

+6
c
source share
7 answers

Using Visual C ++ is one way to create your C project on Windows. However, this is not the only way.

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

There are other options:

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

If I were you, I would try to use as many different build tools as possible.

+3
source share

See also CMake.

+3
source share

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

+2
source share

For simple console applications (and even more complex ones like the Qt framework), you can create on Windows using MinGW . Building with MinGW is pretty similar to building on Linux, and it is unlikely that you can use the same Makefile without any minor changes. This has the main advantage that you do not need to support two sets of Makefile / project files.

You can also use Cygwin , which is basically a Linux API emulation layer for essential functionality. However, the use of Cygwin has the disadvantage that it refers to its own cygwin1.dll library. This may not be a problem for you, as you can simply send this DLL to your program. You can also suppress this with the -mno-cygwin option as a compiler option for gcc / g++ . However, some functions require cygwin1.dll .

I can recommend MinGW for creating programs on Windows that were originally written for Linux. I use this for various command line programs, and for GUI applications I use the Qt framework with the MinGW compiler. If you want to create graphical applications, you definitely need to use a platform with an independent graphical interface, as this simplifies the work.

+2
source share

CMake can help you keep your build portable across multiple toolchains. However, you still have to deal with incompatibilities between languages ​​and libraries.

That is why I recommend using GCC, autotools, etc. on all platforms. In windows, the GNU toolchain is called MinGW / MSys. See an example of MSysGit.

+1
source share

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

You can then create all your Windows and Linux files at once on the same machine from one Makefile. Don't get confused trying to get GNU Make to work properly on Windows, not to mention worrying about Visual Studio.

+1
source share

Yes. It is so simple. Just keep in mind all the time when you need to #ifdef all OS-specific content in #ifdef s.

0
source share

All Articles