C++ Cross-Compiler from Windows to Linux

C++: How to cross-compile from Windows to Linux?

Several comments mentioned using Windows Subsystem for Linux. I would personally recommend this as it is far easier than trying to use a cross-compiler. It also comes with the added benefit that you can test your code in the same environment in which you compile it.

https://docs.microsoft.com/en-us/windows/wsl/about is a great resource for getting started with WSL.

Is it possible to compile from Windows to Linux with gcc/g++?

You have several choices:

  1. WSL.
    WSL(Windows Subsystem for Linux) its linux termanal in windows, so you can compile linux code in windows. This solution is the simpliest and I would recommend to use it.
  2. Visual studio.
    Visual studio has a package that allows you to compile programs for Linux. More details here

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
[...]

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.

How to compile C code in Linux to run on Windows?

You would need a cross-compiler to create a Windows executable in Linux.

Mingw-w64 is an advancement of the original mingw.org project, created to support the GCC compiler on Windows systems.

Installing the cross-compilation

sudo apt-get install mingw-w64

32bit

i686-w64-mingw32-gcc -o test.exe test.c

64bit

x86_64-w64-mingw32-gcc -o test.exe test.c

Cross compiling source from windows to linux

We also need to see some code, but....

Windows has its own types for basic things above the vanilla C ones, as does Linux. It sounds like DWORD as a type is not known in Linux (likely). You'll probably have to create a mytypes.h file that redefines Windows standards like DWORD into Linux speak when building for a Linux platform. Linux has types.h that defines things like int32_t which is the equivalent. See this thread for more about this.

I've assumed you have a working cross compiler set up and you're fighting just with the port. If you haven't, that's your first job. You could have a windows based compiler that targets Linux (the cygwin option, mentioned in another post) or go for a Linux based compiler and targetting windows (crosstool will help here). Though since you seem to be targetting arm, I'm expecting that that Ubuntu install isn't the place you wish to build! :-)



Related Topics



Leave a reply



Submit