Why do I need a Cross Compiler?: Difference between revisions

Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
m (Add link to Host Triplet article)
m (Add links to libgcc article)
Line 7: Line 7:
</source>
</source>


If you are developing on 64-bit Linux, then you will get a response such as 'x86_64-unknown-linux-gnu'. This means that the compiler thinks it is creating code for Linux. If you use this gcc to build your kernel, it will use your system libraries, headers, the Linux libgcc, and it will make a lot of problematic Linux assumptions. If you use a [[GCC_Cross-Compiler|cross-compiler]] such as i585-elf-gcc, then you get a response back such as 'i586-elf' that means the compiler knows it is doing something else and you can avoid a lot of problems easily and properly.
If you are developing on 64-bit Linux, then you will get a response such as 'x86_64-unknown-linux-gnu'. This means that the compiler thinks it is creating code for Linux. If you use this gcc to build your kernel, it will use your system libraries, headers, the Linux [[libgcc]], and it will make a lot of problematic Linux assumptions. If you use a [[GCC_Cross-Compiler|cross-compiler]] such as i585-elf-gcc, then you get a response back such as 'i586-elf' that means the compiler knows it is doing something else and you can avoid a lot of problems easily and properly.


= How to build a Cross-Compiler =
= How to build a Cross-Compiler =
Line 33: Line 33:


=== -lgcc ===
=== -lgcc ===
You disable the important libgcc library when you pass -nostdlib (-nodefaultlibs). The compiler needs this library for many operations that it cannot do itself or that is more efficient to put into a shared function. You must pass this library near the end of the link line, after all the other object files and libraries, or the linker won't use it and you get strange linker errors.
You disable the important [[libgcc]] library when you pass -nodefaultlibs (implied by -nostdlib). The compiler needs this library for many operations that it cannot do itself or that is more efficient to put into a shared function. You must pass this library near the end of the link line, after all the other object files and libraries, or the linker won't use it and you get strange linker errors.


=== -mno-red-zone (x86_64 only) ===
=== -mno-red-zone (x86_64 only) ===
Line 86: Line 86:


=== Reimplementing libgcc ===
=== Reimplementing libgcc ===
You cannot use the host libgcc when building a kernel. The Linux libgcc has some nasty dependencies last timed I checked. The common case newbies run into is 64-bit integer division on 32-bit systems, but the compiler may generate such calls in many cases. You will often end up rewriting libgcc when you should have been using the real thing in the first place.
You cannot use the host [[libgcc]] when building a kernel. The Linux [[libgcc]] has some nasty dependencies last timed I checked. The common case newbies run into is 64-bit integer division on 32-bit systems, but the compiler may generate such calls in many cases. You will often end up rewriting [[libgcc]] when you should have been using the real thing in the first place.


=== Rewriting freestanding headers (often incorrectly) ===
=== Rewriting freestanding headers (often incorrectly) ===