C++ cross-compiler from Windows to Linux

C++WindowsLinuxCross Compiling

C++ Problem Overview


Are there any user friendly tools out there to cross-compile from Windows to Linux?

C++ Solutions


Solution 1 - C++

crosstool-ng supports building cross-compilers with cygwin host.

Overview:

  • Install cygwin, with development packages
  • Download crosstool-ng
  • Extract the tarball (tar xvjf crosstool-ng*)
  • Run ./configure
  • When configure complains about a missing tool, go back to cygwin setup and install the corresponding package (e.g. flex, libtool, ncurses-devel) You'll need at least the following:
  • Devel/gperf
  • Devel/bison
  • Devel/byacc
  • Devel/wget
  • Devel/patch
  • Devel/make (GNU version)
  • Devel/automake 1.10+
  • Libs/libncursesw10
  • Libs/libncursesw10-devel
  • make
  • make install
  • Create a new directory for building the cross-compiler, e.g. /usr/src/cross-linux-gnu-root
  • Enable system-wide case sensitivity support in the registry (see https://superuser.com/questions/266110/how-do-you-make-windows-7-fully-case-sensitive-with-respect-to-the-filesystem#answer-842670)
  • Mount the directory case-sensitive (e.g. mount c:/cygwin/usr/src/cross-linux-gnu-root /usr/src/cross-linux-gnu -o binary,posix=1)
  • From inside the cross-compiler build directory, ct-ng menuconfig
  • In the menus, set target architecture to x86 (probably) and subarchitecture to i686 (avoids GCC 4.8 issues, thanks osm0sis), target kernel to linux, and target C library to glibc, and enable the C++ compiler.
  • To work around make 4.0 issues, also enable EXPERIMENTAL in Paths and misc options then, go in Companion tools (at top-level) and enable Build some companion tools and then make 3.81 (Thanks osm0sis)
  • wget has issues with the latest kernel.org certificates so use the .wgetrc method in this accepted answer: https://stackoverflow.com/questions/9224298/how-do-i-fix-certificate-errors-when-running-wget-on-an-https-url-in-cygwin (Thanks osm0sis)
  • Currently 3 file patches are required to avoid further errors:
  • ct-ng build

Of course, this is NOT going to enable you to build linux applications from inside Visual Studio. (VS2010 and later let you build with other toolchains such as gcc, but you'd need an appropriate toolchain description in addition to the cross-compiler built by crosstool-ng). But you'll have a working g++-linux-gnu, which you can either run directly or using a Makefile.

NOTE: Building the cross-compiler takes a LONG time. osm0sis has provided a prebuilt gcc 4.8.1 here, along with his notes on building the cross-compiler (used to update this answer).

Solution 2 - C++

Your best bet is to use a cross platform IDE like Code::Blocks that can import MSVC projects, and generate a Makefile for Linux, which you can then run on a Linux (with even the same program if you so wish).

So your work flow would then look something like the following:

Code, Compile, and debug on MSVC -> import into Code::Blocks & generate Makefile
-> test compile and debug with GCC
-> copy to Linux with Code::Blocks -> test compile & debug on Linux

It's a little obtuse, probably, but at least Code::Blocks takes care of a lot of those small details with things like Makefiles, and what not.

Solution 3 - C++

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. The fastest switching between Windows and Linux through Alt+Tab (no restarting or rebooting needed):

  1. Install CoLinux
  2. Use CoLinux root file system image: Ubuntu-9.04-1gb.7z
  3. Start CoLinux
  4. Change string archive.ubuntu.com to old-releases.ubuntu.com with:
$ sudo vi /etc/apt/sources.list
  1. Update your configuration:
$ sudo apt-get update
  1. Install two software packages to be able to compile your programs (c/c++, gcc compilers, mysql dev 5.1):
$ sudo apt-get install build-essential
$ sudo apt-get install libmysqlclient15-dev
  1. Accessing Windows directories from CoLinux:
$ mount cofs0 /mnt/ -t cofs
$ cd /mnt
  1. Compile
$ c++ your.source.cc -o executable.name

Solution 4 - C++

Are you asking for something that can take code written to run on Windows and make it run on linux? There's not really a compiler that does that - but maybe you're looking for Wine ( http://www.winehq.org/ ) which lets you run Windows apps on linux (if they use APIs Wine has thunked out).

Edit: I'm assuming your code is using Windows APIs (Win32, etc.) in this answer. If you're only using Standard C++ and libraries available cross-platform, then there's lots of other answers.

Solution 5 - C++

I don't believe there are any compilers that allow you to compile on Windows and then take the resulting binary and run it on a Linux machine. Your best bet is to set up two development environments, one on Windows and one on Linux. Do most of your development and debugging in the Windows environment if that's what suits you, then frequently compile and test under Linux to be sure your code is truly cross-platform. You'll also want to use a cross-platform build system and testing framework. I recommend using [cmake][1] and [Google Test][2], respectively.

[1]: http://www.cmake.org/ "cmake" [2]: http://code.google.com/p/googletest/ "Google Test"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionneo_x3mView Question on Stackoverflow
Solution 1 - C++Ben VoigtView Answer on Stackoverflow
Solution 2 - C++supercheetahView Answer on Stackoverflow
Solution 3 - C++Angel TView Answer on Stackoverflow
Solution 4 - C++Austin LambView Answer on Stackoverflow
Solution 5 - C++DarrylView Answer on Stackoverflow