Manual for Cross-Compiling a C++ Application from Linux to Windows

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

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.

Cross compile windows 64 bit .exe from linux

It looks like my answer lies with the Mingw-w64 project which is available for host OSes Linux, Darwin & Windows

How do I cross-compile C code on Windows for a binary to also be run on Unix (Solaris/HPUX/Linux)?

Cross-compiler are very hard to setup and get working correctly.

Consider that (the people at) NetBSD have to put in a huge amount of work to get cross-compiling to work, and they're running the same OS, just different architectures.

You'd have to, at least, copy all the headers from the other OSs to Windows, and get a cross-compiler, linker etc for the target OS/architecture.

Also that may well not be possible - perl and shared libraries may be compiled with a native/non-gcc compiler which won't be available on Windows at all.

How to compile for Windows on Linux with gcc/g++?

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.

Ubuntu, for example, has MinGW in its repositories:

$ apt-cache search mingw
[...]
g++-mingw-w64 - GNU C++ compiler for MinGW-w64
gcc-mingw-w64 - GNU C compiler for MinGW-w64
mingw-w64 - Development environment targeting 32- and 64-bit Windows
[...]

Cross-compile a Rust application from Linux to Windows

Other answers, while technically correct, are more difficult than they need to be. There's no need to use rustc (in fact it's discouraged, just use cargo), you only need rustup, cargo and your distribution's mingw-w64.

Add the target (you can also change this for whatever target you're cross compiling for):

rustup target add x86_64-pc-windows-gnu

You can build your crate easily with:

cargo build --target x86_64-pc-windows-gnu

No need for messing around with ~/.cargo/config or anything else.

EDIT: Just wanted to add that while you can use the above it can also sometimes be a headache. I wanted to add that the rust tools team also maintains a project called cross: https://github.com/rust-embedded/cross
This might be another solution that you want to look into

wxWidgets how to cross compile an application for windows from linux using codeblocks?

These are the steps I followed on Ubuntu 13.10 (Saucy Salamander)

Update the system and install mingw32 and wine:

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install mingw32 wine

Install wxWidgets 2.8 from source:

sudo su -
cd /opt
wget http://downloads.sourceforge.net/project/wxwindows/2.8.12/wxWidgets-2.8.12.tar.gz
tar -xvzf wxWidgets-2.8.12.tar.gz
cd wxWidgets-2.8.12/
./configure --prefix=/usr/local/i586-mingw32 --host=i586-mingw32msvc --build=i686-linux --disable-shared --enable-unicode
make
make install

Install codeblocks

sudo apt-get install codeblocks codeblocks-contrib

Open codeblocks, then setup as follows:

Settings > Compiler > 

Selected Compiler > GNU GCC Compiler > Copy > New Compiler Name : MinGW Compiler

Selected Compiler > MinGW Compiler

Search Directories > Compiler > Add : /usr/i586-mingw32msvc/include/
Search Directories > Linker > Add : /usr/i586-mingw32msvc/lib/
Search Directories > Resource Compiler > Add : /usr/i586-mingw32msvc/include/

Toolchain Executables >
Program Files >
Compilers Installation Directory : /usr/i586-mingw32msvc/
C Compiler : i586-mingw32msvc-gcc
C++ Compiler : i586-mingw32msvc-g++
Dynamic Linker : i586-mingw32msvc-g++
Static Linker : i586-mingw32msvc-ld
Additional Paths : /usr/bin

Compiler Settings > Other Options : `/usr/local/i586-mingw32/bin/wx-config --cxxflags`
Linker Settings > Other Options : `/usr/local/i586-mingw32/bin/wx-config --libs`

Now create a new project in codeblocks, the key fields are as follows:

New Project > wXWidgets Project
wXWidgets > 2.8.x
Preferred GUI Builder > wxSmith
Compiler > MinGW Compiler

In Codeblocks, click on compile - this sets up the applications output folder which we need in the next step.

In a terminal window, copy required mingwm10.dll to output folder:

cd <your applications Debug folder>
cp /usr/share/doc/mingw32-runtime/mingwm10.dll.gz .
gunzip mingwm10.dll.gz

Now run with wine:

wine ./youappname

To run your application on windows machine, copy mingwm10.dll and your appname executable to a windows machine. Rename your appname to have the suffix .exe. Double click to run.



Related Topics



Leave a reply



Submit