Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-compiling on Windows and Linux

Tags:

c

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 as 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?

like image 469
ant2009 Avatar asked Jan 07 '10 06:01

ant2009


People also ask

How do I cross-compile from Windows to Linux?

Compile to Linux from Windows without to use virtualization or cross compiler but only natively via CoLinux. Create native Linux executable files without leave (reboot) windows. C/C++ Compiling for Linux under Windows through Cooperative Linux.

Can you compile Windows programs on Linux?

mingw32 exists as a package for Linux. You can cross-compile and -link Windows applications with it. There's a tutorial here at the Code::Blocks forum. Mind that the command changes to x86_64-w64-mingw32-gcc-win32 , for example.

What is cross compiling Linux?

Cross-compilation is the act of compiling code for one computer system (often known as the target) on a different system, called the host. It's a very useful technique, for instance when the target system is too small to host the compiler and all relevant files.

Can GCC compile to Windows?

GCC (and GNU Toolchain) is currently available on all Unixes. They are also ported to Windows (by Cygwin, MinGW and MinGW-W64). GCC is also a cross-compiler, for producing executables on different platform.


1 Answers

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.

like image 80
Assaf Lavie Avatar answered Sep 22 '22 21:09

Assaf Lavie