What you're asking for, as Thomas Dickey posted, is a cross compiler.
They're not difficult to do, but they are very tedious to set up and keep set up properly, because there are a lot of dependencies on the target system that have to be accounted for in order to build a proper executable.
What are some of those dependencies? Just off the top of my head:
- First, your target system is likely a hosted environment so your cross compiler would need to provide executable start-up code/binaries to allow the target system to actually get to the point of calling your
- There are a lot of implementation-defined details on the target system, such as "How big is a
long". Those can differ, so you need, for lack of a better word - the target implementation. In general, that means at least all the header files for your target system that are "part of the implementation". Determining which ones you need and which ones you don't is a tedious task at best. So just grab all of them, right? Well, that increases the number of dependencies you have to worry about.
- So now you can compile - but you can't link without the target systems libraries (this relates back to #1 above, but even more is needed). While in theory you can link with just the target systems static libraries and without the target systems dynamic libraries, without all the libraries you can't really be certain the binary produced by your cross-compiler won't fail with a "missing symbol" error when you try to run it on your target system.
So you need the header files, the libraries, and the startup binaries/code for your target system to create a cross-compiler - and once you get it set up, you have to maintain it - if the target system gets patched in a way that impacts your cross-compiler, you need to replicate those changes in your compiler. How do you determine that patch "123456" or RPM "abc" impacts your cross compiler?
And I probably missed a whole lot.
It should be pretty obvious why no one bothers with cross compilers where the target system is something that's easy to create an instance of like Linux/Solaris/Windows/BSD on x86 hardware, and even why when the target system is something harder (like Solaris on SPARC) almost everyone with a need to compile for that target just buys some low-end compatible hardware for compiling on.