Are there any user friendly tools out there to cross-compile from Windows to Linux?
-
15Funny: all that stuff going through the VS IDE is what confuses the heck out of me - there's so much configuration required to get it to work. It's so much easier on Linux where you don't have it! – Jonathan Leffler Jan 22 '11 at 19:24
-
9Usually "a cross-compiler from X to Y" would mean, "a compiler that itself runs on X, but produces binaries that run on Y". So you're asking for a compiler that runs on Windows and produces Linux binaries. Is that what you want? The usual way to do it is to compile for linux on linux. The usual reason for cross-compiling is that Y is not a viable development environment (e.g. because it's a mobile phone, set-top box, Roomba, etc), maybe you'd be better off finding a Linux IDE you can live with? – Steve Jessop Jan 22 '11 at 19:31
-
you got it Steve thanks for the X Y explanation you did a better job than me , but I must replay NO , that's not a solution for me , I relay need a compiler that runs on windows and generates linux binary. – neo_x3m Jan 22 '11 at 19:40
-
@neo_x3m, I do not think you can do that, even using Cygwin (which is simply Linux under Windows!) will not solve the problem of generating a linux library and linking against it under Windows. – Y.H. Jan 22 '11 at 19:45
-
3@ H.Josef come-one you can compile code for an avr uControler with a compiler running under windows and you can't compile code for linux using a compiler that runs under windows ??? I don't believe that . – neo_x3m Jan 22 '11 at 19:52
-
I feel the need to mention that Visual Studio is the IDE; the compiler itself is separate and can be run from the command line or a make program. – Stephen P Jan 22 '11 at 20:07
-
1@neo_x3m AFAIK you can compile standard C++ code using any compiler, but creating a library (and testing it) on a completely different operating system is cumbersome and even impossible, Once I tried to create and test a dynamic linux library (.so) using MingW and it did not work, it did not work even with Cygwin since Windows environment is very different from Linux. – Y.H. Jan 22 '11 at 20:08
-
@H.Josef: it should be possible under Cygwin by setting gcc's target to the appropriate linux configuration. gcc should be able to produce code for pretty much any platform, running on any platform, provided that you have the necessary native code generator and executable file format thingy installed. As to how that's actually achieved, though, in terms of what you have to install and what options you need - no idea. And all this assumes that the code is portable - if it calls Windows functions then of course it doesn't just need to be *compiled* for linux, it needs to be *ported* to linux. – Steve Jessop Jan 23 '11 at 17:24
-
2_Running_ the Linux binary would be very cumbersome, but if you have all the libs/OBJ in Linux format, but compiling is nothing magical... if a Linux compiler app would _run_ on Windows, why shouldn't it work? Why couldn't you even compile a Linux compiler _under_ Windows? – Mr. Boy Feb 14 '11 at 18:42
-
programming a bit, "compiling" with ffive i guess. anyway it is possible, but you first have to compile a crosscompiler. the most complete explanaition i found http://www.gentoo-wiki.info/HOWTO_Distcc_server_on_Windows . i know it is about distcc, but it covers the setup of a xcompiler... – relascope Jun 13 '15 at 22:17
5 Answers
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 toi686
(avoids GCC 4.8 issues, thanks osm0sis), target kernel tolinux
, and target C library toglibc
, and enable the C++ compiler. - To work around make 4.0 issues, also enable EXPERIMENTAL in
Paths and misc options
then, go inCompanion tools
(at top-level) and enableBuild some companion tools
and thenmake 3.81
(Thanks osm0sis) - wget has issues with the latest kernel.org certificates so use the .wgetrc method in this accepted answer: 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:
- the _libintl_gettext error as mentioned in the comments
- the byteshift headers, and
- the ELF headers (Thanks Duncan Calvert & osm0sis)
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).
-
Is it possible to create and link against dynamic Linux libraries using this approach? if not, then why not just sticking to the easier-to-deply MingW which compiles the code to Windows/Linux binaries? – Y.H. Jan 22 '11 at 21:19
-
6What's a "Windows/Linux" binary? AFAIK, mingw is just gcc-for-windows, and makes PE executables. Linux binaries must be in linux-compatible formats, of which ELF is the most popular. I'm in the middle of the `ct-ng build` step now, trying to make sure my answer is as complete as possible and includes solutions to any gotchas along the way. And yes, the compiler I'm building will generate ELF executables which are dynamically linked against glibc (and other libraries as appropriate). – Ben Voigt Jan 22 '11 at 21:30
-
What I meant is standard code that can be compiled either on GCC/Linux, or MingW/Windows. +1 for clarification about the different binary formats. – Y.H. Jan 22 '11 at 21:42
-
I had [a _libintl_gettext error](http://stackoverflow.com/questions/13881633/error-when-installing-crosstool-ng-on-cygwin-libintl-gettext) and fixed it here: http://sourceware.org/ml/crossgcc/2012-05/msg00012.html – Duncan Calvert Jun 22 '13 at 17:59
-
While mounting the directory I got `mount: warning: couldn't determine mount type.` Still unresolved. – Duncan Calvert Jun 22 '13 at 19:31
-
user Osmosis mentions that http://stackoverflow.com/questions/9224298/how-do-i-fix-certificate-errors-when-running-wget-on-an-https-url-in-cygwin may be helpful if the ct-ng build fails to download component packages – Ben Voigt Mar 09 '14 at 16:24
-
Are there similar cross-compilation steps documented somewhere for using clang on windows to produce binaries that run on linux? – Raman Sharma Dec 10 '14 at 22:44
-
There is a problem with using prebuilt version from osm0sis - when I try to extract the archive, I got an alert that some files already exist in the folder. That's because some headers (I don't know how important they are) have names which differ only in case. I'm not sure how important that issue is. – Amomum Jan 12 '16 at 13:40
-
Also, I believe Intel Edison SDK (found here https://software.intel.com/en-us/iot/hardware/edison/downloads SDK - Cross Compile Tools) also contains prebuilt version of cross gcc **and it doesn't require cygwin**! But it has the same problem with file-names. – Amomum Jan 12 '16 at 13:43
-
@Amomum: Enabling filesystem case-sensitivity is right there in my list. The only "problem" is you not following the instructions. – Ben Voigt Jan 12 '16 at 14:37
-
@BenVoigt I hoped that step is not necessary for using pre-built version and your instruction is for building cross-compiler, not for using it. The problem is also present for Intel SDK, which doesn't require cygwin and has no reference of case sensitivity. – Amomum Jan 12 '16 at 15:08
-
This would be a "good" start, but generally what would you like to do ? In most cases, you want to develop some linux applications for a linux target system (and may be a linux distribution e.g. opensuse 13.2) . In the case you want to compile for a linux distribution, you will need a compiler crosscompiled with the same kernel release of your target system and the same glibc library with same kernel settings and a filesystem copy of the target distribution. – sancelot Aug 03 '16 at 09:22
-
@sancelot glibc cares about kernel version somewhat, but the compiler doesn't. – Ben Voigt Aug 03 '16 at 13:47
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):
- Install CoLinux
- Use CoLinux root file system image: Ubuntu-9.04-1gb.7z
- Start CoLinux
- Change string
archive.ubuntu.com
toold-releases.ubuntu.com
with:
$ sudo vi /etc/apt/sources.list
- Update your configuration:
$ sudo apt-get update
- 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
- Accessing Windows directories from CoLinux:
$ mount cofs0 /mnt/ -t cofs
$ cd /mnt
- Compile
$ c++ your.source.cc -o executable.name

- 2,309
- 2
- 31
- 45

- 182
- 2
- 7
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.

- 3,200
- 4
- 25
- 38
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.

- 3,116
- 1
- 22
- 13
-
3`winelib` is a good suggestion in case the code is full of Win32-isms. But saying there's no compiler to build Linux binaries is baloney. – Ben Voigt Jan 22 '11 at 20:52
-
3To build linux binaries that magically thunk out all Windows APIs? I think it's a valid statement to say there's no compiler that does that - it's not a compiler's job to get APIs from one OS to work identically on another OS. – Austin Lamb Jan 22 '11 at 20:57
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 and Google Test, respectively.

- 5,907
- 1
- 25
- 36
-
3g++ supports an awful lot of cross and canadian cross configurations. – Ben Voigt Jan 22 '11 at 21:13
-
I would still strongly recommend setting up a cross-platform build system, then building on each of the target platforms natively. Setting up a VM is a heck of a lot easier than the crosstool-ng option described below. Not only that, but tools like CMake allow you to use VS for debugging on windows, which the OP prefers. – Darryl Apr 26 '14 at 14:16
-
2This answer is wrong. There are multiple options for compiling Linux code on Windows. The biggest headache is dealing with libs. For example, a build build on cygwin may require libphp5.so of version 5.4.7 when Ubuntu may only have 5.4.8. Not a huge deal, just compile 5.4.7 on ubuntu and you'll be good, but it;s not exactly a zip-bam-boom solution. – JSON Nov 12 '14 at 21:41
-
@JSON, Although you are right that there are options I was not aware of, the example you give is not one of them. Binaries compiled for cygwin don't run in Linux. See http://stackoverflow.com/questions/4143629/executable-file-generated-using-gcc-under-cygwin. – Darryl Nov 14 '14 at 16:27
-
1@Darryl - Android NDK is a good example of a cgywin based cross compiler that builds Linux compatible binaries on Windows. It's true that they are not standard cgywin based libs, but that's the point of any cross compiler in the end. – JSON Nov 14 '14 at 20:52