Program execution |
---|
General concepts |
Types of code |
Compilation strategies |
Notable runtimes |
|
Notable compilers & toolchains |
|
Across compiler is acompiler capable of creatingexecutable code for aplatform other than the one on which the compiler is running. For example, a compiler that runs on aPC but generates code that runs onAndroid devices is a cross compiler.
A cross compiler is useful to compile code for multiple platforms from one development host. Direct compilation on the target platform might be infeasible, for example onembedded systems with limited computing resources.
Cross compilers are distinct fromsource-to-source compilers. A cross compiler is forcross-platform software generation of machine code, while a source-to-source compiler translates from one coding language to another in text code. Both areprogramming tools.
The fundamental use of a cross compiler is to separate the build environment from target environment. This is useful in several situations:
Use ofvirtual machines (such as Java'sJVM) resolves some of the reasons for which cross compilers were developed. The virtual machine paradigm allows the same compiler output to be used across multiple target systems, although this is not always ideal because virtual machines are often slower and the compiled program can only be run on computers with that virtual machine.
Typically thehardware architecture differs (e.g. coding a program destined for theMIPS architecture on anx86 computer) but cross-compilation is also usable when only theoperating system environment differs, as when compiling aFreeBSD program underLinux, or even just the system library, as when compiling programs withuClibc on aglibc host.
TheCanadian Cross is a technique for building cross compilers for other machines, where the original machine is much slower or less convenient than the target. Given three machines A, B, and C, one uses machine A (e.g. runningWindows XP on anIA-32 processor) to build a cross compiler that runs on machine B (e.g. runningmacOS on anx86-64 processor) to create executables for machine C (e.g. runningAndroid on anARM processor). The practical advantage in this example is that Machine A is slow but has a proprietary compiler, while Machine B is fast but has no compiler at all, and Machine C is impractically slow to be used for compilation.
When using the Canadian Cross with GCC, and as in this example, there may be four compilers involved
The end-result cross compiler (4) will not be able to run on build machine A; instead it would run on machine B to compile an application into executable code that would then be copied to machine C and executed on machine C.
For instance,NetBSD provides aPOSIXUnix shell script namedbuild.sh
which will first build its owntoolchain with the host's compiler; this, in turn, will be used to build the cross compiler which will be used to build the whole system.
The termCanadian Cross came about because at the time that these issues were under discussion, Canada had three national political parties.[1]
![]() | This sectionneeds expansion. You can help byadding to it.(July 2012) |
GCC, afree software collection of compilers, can be set up to cross compile. It supports many platforms and languages.
GCC requires that a compiled copy ofbinutils is available for each targeted platform. Especially important is theGNU Assembler. Therefore, binutils first has to be compiled correctly with the switch--target=some-target
sent to theconfigure script. GCC also has to beconfigured with the same--target
option. GCC can then be run normally provided that the tools, whichbinutils creates, are available in thepath, which can be done using the following (on UNIX-like operating systems with bash):
PATH=/path/to/binutils/bin:${PATH} make
Cross-compiling GCC requires that a portion of thetarget platform'sC standard library be available on thehost platform. The programmer may choose to compile the full C library, but this choice could be unreliable. The alternative is to usenewlib, which is a smallC library containing only the most essential components required to compileC source code.
TheGNU Autotools packages (i.e.autoconf,automake, andlibtool) use the notion of abuild platform, ahost platform, and atarget platform. Thebuild platform is where the compiler is actually compiled. In most cases, build should be left undefined (it will default from host). Thehost platform is always where the output artifacts from the compiler will be executed whether the output is another compiler or not. Thetarget platform is used when cross-compiling cross compilers, it represents what type of object code the package will produce; otherwise thetarget platform setting is irrelevant.[2] For example, consider cross-compiling a video game that will run on aDreamcast. The machine where the game is compiled is thebuild platform while the Dreamcast is thehost platform. The nameshost andtarget are relative to the compiler being used and shifted likeson andgrandson.[3]
Another method popularly used by embedded Linux developers involves the combination of GCC compilers with specialized sandboxes likeScratchbox andScratchbox 2, orPRoot. These tools create a "chrooted" sandbox where the programmer can build up necessary tools, libc, and libraries without having to set extra paths. Facilities are also provided to "deceive" the runtime so that it "believes" it is actually running on the intended target CPU (such as an ARM architecture); this allows configuration scripts and the like to run without error. Scratchbox runs more slowly by comparison to "non-chrooted" methods, and most tools that are on the host must be moved into Scratchbox to function.
Manx Software Systems, ofShrewsbury,New Jersey, producedC compilers beginning in the 1980s targeted at professional developers for a variety of platforms up to and includingIBM PC compatibles andMacs.
Manx'sAztec Cprogramming language was available for a variety of platforms includingMS-DOS,Apple II,DOS 3.3 andProDOS,Commodore 64,Mac 68k[4] andAmiga.
From the 1980s and continuing throughout the 1990s until Manx Software Systems disappeared, the MS-DOS version of Aztec C[5] was offered both as a native mode compiler or as a cross compiler for other platforms with different processors including the Commodore 64[6] and Apple II.[7] Internet distributions still exist for Aztec C including their MS-DOS based cross compilers. They are still in use today.
Manx's Aztec C86, their native mode8086 MS-DOS compiler, was also a cross compiler. Although it did not compile code for a different processor like their Aztec C656502 cross compilers for the Commodore 64 and Apple II, it created binary executables for then-legacy operating systems for the 16-bit 8086 family of processors.
When the IBM PC was first introduced it was available with a choice of operating systems,CP/M-86 andPC DOS being two of them. Aztec C86 was provided with link libraries for generating code for bothIBM PC operating systems. Throughout the 1980s later versions of Aztec C86 (3.xx, 4.xx and 5.xx) added support forMS-DOS "transitory" versions 1 and 2[8] and which were less robust than the "baseline" MS-DOS version 3 and later which Aztec C86 targeted until its demise.
Finally, Aztec C86 provided C language developers with the ability to produceROM-able"HEX" code which could then be transferred using aROM burner directly to an 8086 based processor.Paravirtualization may be more common today but the practice of creating low-level ROM code was more common per-capita during those years whendevice driver development was often done by application programmers for individual applications, and new devices amounted to acottage industry. It was not uncommon for application programmers to interface directly with hardware without support from the manufacturer. This practice was similar toEmbedded Systems Development today.
Thomas Fenwick and James Goodnow II were the two principal developers of Aztec-C. Fenwick later became notable as the author of theMicrosoftWindows CEkernel or NK ("New Kernel") as it was then called.[9]
Microsoft C (MSC) has a shorter history than others[10] dating back to the 1980s. The first Microsoft C Compilers were made by the same company who madeLattice C and were rebranded by Microsoft as their own, until MSC 4 was released, which was the first version that Microsoft produced themselves.[11]
In 1987, many developers started switching to Microsoft C, and many more would follow throughout the development of Microsoft Windows to its present state. Products likeClipper and laterClarion emerged that offered easy database application development by using cross language techniques, allowing part of their programs to be compiled with Microsoft C.
Borland C (California company) was available for purchase years before Microsoft released its first C product.
Long before Borland, BSD Unix (Berkeley University) had gotten C from the authors of the C language:Kernighan andRitchie who wrote it in unison while working forAT&T (labs). K&R's original needs was not only elegant 2nd level parsed syntax to replace asm 1st level parsed syntax: it was designed so that a minimal amount of asm be written to support each platform (the original design of C was ability to cross compile using C with the least support code per platform, which they needed.). Also yesterdays C directly related to ASM code wherever not platform dependent. Today's C (more-so c++) is no longer C compatible and the asm code underlying can be extremely different than written on a given platform (in Linux: it sometimes replaces and detours library calls with distributor choices). Today's C is a 3rd or 4th level language which is used the old way like a 2nd level language.
C programs had long been linked with modules written inassembly language. Most C compilers (even current compilers) offer an assembly language pass (that can be tweaked for efficiency then linked to the rest of the program after assembling).
Compilers like Aztec-C converted everything to assembly language as a distinct pass and then assembled the code in a distinct pass, and were noted for their very efficient and small code, but by 1987 the optimizer built into Microsoft C was very good, and only "mission critical" parts of a program were usually considered for rewriting. In fact, C language programming had taken over as the "lowest-level" language, with programming becoming a multi-disciplinary growth industry and projects becoming larger, with programmers writing user interfaces and database interfaces in higher-level languages, and a need had emerged for cross language development that continues to this day.
By 1987, with the release of MSC 5.1, Microsoft offered a cross language development environment for MS-DOS. 16-bit binary object code written in assembly language (MASM) and Microsoft's other languages includingQuickBASIC,Pascal, andFortran could be linked together into one program, in a process they called "Mixed Language Programming" and now "InterLanguage Calling".[12] IfBASIC was used in this mix, the main program needed to be in BASIC to support the internalruntime system that compiled BASIC required for garbage collection and its other managed operations that simulated a BASICinterpreter likeQBasic in MS-DOS.
Thecalling convention for C code, in particular, was to pass parameters in "reverse order" on thestack and return values on the stack rather than in aprocessor register. There were other programming rules to make all the languages work together, but this particular rule persisted through the cross language development that continued throughoutWindows 16- and 32-bit versions and in the development of programs forOS/2, and which persists to this day. It is known as thePascal calling convention.
Another type of cross compilation that Microsoft C was used for during this time was in retail applications that requirehandheld devices like theSymbol Technologies PDT3100 (used to takeinventory), which provided a link library targeted at an8088 basedbarcode reader. The application was built on the host computer then transferred to the handheld device (via aserial cable) where it was run, similar to what is done today for that same market usingWindows Mobile by companies likeMotorola, who bought Symbol.
Throughout the 1990s and beginning with MSC 6 (their firstANSI C compliant compiler) Microsoft re-focused their C compilers on the emerging Windows market, and also onOS/2 and in the development ofGUI programs. Mixed language compatibility remained through MSC 6 on the MS-DOS side, but theAPI for Microsoft Windows 3.0 and 3.1 was written in MSC 6. MSC 6 was also extended to provide support for 32-bit assemblies and support for the emergingWindows for Workgroups andWindows NT which would form the foundation forWindows XP. A programming practice called athunk was introduced to allow passing between 16- and 32-bit programs that took advantage of runtime binding (dynamic linking) rather than thestatic binding that was favoured inmonolithic 16-bit MS-DOS applications. Static binding is still favoured by some native code developers but does not generally provide the degree ofcode reuse required by newer best practices like theCapability Maturity Model (CMM).
MS-DOS support was still provided with the release of Microsoft's first C++ Compiler, MSC 7, which was backwardly compatible with the C programming language and MS-DOS and supported both 16- and 32-bit code generation.
MSC took over whereAztec C86 left off. The market share for C compilers had turned to cross compilers which took advantage of the latest and greatest Windows features, offered C and C++ in a single bundle, and still supported MS-DOS systems that were already a decade old, and the smaller companies that produced compilers like Aztec C could no longer compete and either turned to niche markets likeembedded systems or disappeared.
MS-DOS and 16-bit code generation support continued until MSC 8.00c which was bundled with Microsoft C++ and Microsoft Application Studio 1.5, the forerunner ofMicrosoft Visual Studio which is the cross development environment that Microsoft provide today.
MSC 12 was released with Microsoft Visual Studio 6 and no longer provided support for MS-DOS 16-bit binaries, instead providing support for 32-bit console applications, but provided support forWindows 95 andWindows 98 code generation as well as forWindows NT. Link libraries were available for other processors that ran Microsoft Windows; a practice that Microsoft continues to this day.
MSC 13 was released withVisual Studio 2003, and MSC 14 was released withVisual Studio 2005, both of which still produce code for older systems like Windows 95, but which will produce code for several target platforms including the mobile market and theARM architecture.
In 2001 Microsoft developed theCommon Language Runtime (CLR), which formed the core for their.NET Framework compiler in the Visual Studio IDE. This layer on the operating system which is in theAPI allows the mixing of development languages compiled across platforms that run the Windows operating system.
The .NET Framework runtime and CLR provide a mapping layer to the core routines for the processor and the devices on the target computer. The command-line C compiler in Visual Studio will compile native code for a variety of processors and can be used to build the core routines themselves.
Microsoft .NET applications for target platforms likeWindows Mobile on theARM architecture cross-compile on Windows machines with a variety of processors and Microsoft also offer emulators and remote deployment environments that require very little configuration, unlike the cross compilers in days gone by or on other platforms.
Runtime libraries, such asMono, provide compatibility for cross-compiled .NET programs to other operating systems, such asLinux.
Libraries likeQt and its predecessors includingXVT provide source code level cross development capability with other platforms, while still using Microsoft C to build the Windows versions. Other compilers likeMinGW have also become popular in this area since they are more directly compatible with the Unixes that comprise the non-Windows side of software development allowing those developers to target all platforms using a familiar build environment.
Free Pascal was developed from the beginning as a cross compiler. The compiler executable (ppcXXX where XXX is a target architecture) is capable of producing executables (or just object files if no internal linker exists, or even just assembly files if no internal assembler exists) for all OS of the same architecture. For example, ppc386 is capable of producing executables for i386-linux, i386-win32, i386-go32v2 (DOS) and all other OSes (see[13]). For compiling to another architecture, however, a cross architecture version of the compiler must be built first. The resulting compiler executable would have additional 'ross' before the target architecture in its name. i.e. if the compiler is built to target x64, then the executable would be ppcrossx64.
To compile for a chosen architecture-OS, the compiler switch (for the compiler driver fpc) -P and -T can be used. This is also done when cross-compiling the compiler itself, but is set via make option CPU_TARGET and OS_TARGET. GNU assembler and linker for the target platform is required if Free Pascal does not yet have internal version of the tools for the target platform.
Clang is natively a cross compiler, at build time you can select which architectures you want Clang to be able to target.
The heterogenous systemPlan 9 and its toolchain does not distinguish between cross and native compilation.Makefiles are architecture independent.
This is called a `Canadian Cross' because at the time a name was needed, Canada had three national parties.
i386