Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manual for cross-compiling a C++ application from Linux to Windows?

Is there a manual for cross-compiling a C++ application from Linux to Windows?

Just that. I would like some information (links, reference, examples...) to guide me to do that.

I don't even know if it's possible.

My objective is to compile a program in Linux and get a .exe file that I can run under Windows.

like image 869
Pablo Herrero Avatar asked Oct 08 '08 12:10

Pablo Herrero


People also ask

Can GCC cross compile?

For instance when installing GCC, the GNU Compiler Collection, we can use --target= target to specify that we want to build GCC as a cross-compiler for target . Mixing --build and --target , we can cross-compile a cross-compiler; such a three-way cross-compilation is known as a Canadian cross.

What is cross compilation in C?

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.


2 Answers

The basics are not too difficult:

sudo apt-get install mingw32     cat > main.c <<EOF int main() {   printf("Hello, World!"); } EOF i586-mingw32msvc-cc main.c -o hello.exe 

Replace apt-get with yum, or whatever your Linux distro uses. That will generate a hello.exe for Windows.

Once you get your head around that, you could use autotools, and set CC=i586-mingw32msvc-cc

CC=i586-mingw32msvc-cc ./configure && make 

Or use CMake and a toolchain file to manage the build. More difficult still is adding native cross libraries. Usually they are stored in /usr/cross/i586-mingw32msvc/{include,lib} and you would need to add those paths in separately in the configure step of the build process.

like image 112
richq Avatar answered Sep 28 '22 09:09

richq


It depends on what you mean (I couldn't really say).

  1. If you mean that you want to use an existing Linux application on Windows, then you could try compiling it using Cygwin on Windows. This however does not give you a Windows executable free from all dependencies towards Cygwin (your executable still depends on the cygwin.dll file) - and it still may need some porting before it will work. See http://www.cygwin.com.

  2. If you mean that you want to be able to perform the actual compilation of a Windows application on Linux and produce a .exe file that is executable on Windows - thus using your Linux box for development and/or compilation then you should look into MinGW for Linux which is a tool for crosscompiling for Windows on Linux. See http://www.mingw.org/wiki/LinuxCrossMinGW.

Best regards!

like image 21
Anders Hansson Avatar answered Sep 28 '22 08:09

Anders Hansson