"zig cc" Cross-Compiler: Difference between revisions
[unchecked revision] | [unchecked revision] |
(Add first draft of zig cc page) |
No edit summary |
||
Line 1: | Line 1: | ||
== Introduction == |
|||
If you want to write an operating system in C, you'll [[Why_do_I_need_a_Cross_Compiler|need a cross-compiler]]. However, [[GCC_Cross-Compiler|building a GCC cross-compiler]] can be painful. If you just want to write C (not C++), you can use [https://andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html <kbd>zig cc</kbd>] - a C cross-compiler that comes with the [https://ziglang.org/ Zig programming language]. There are [https://ziglang.org/download/ pre-compiled binaries] for Windows, Linux, MacOS, and FreeBSD. |
If you want to write an operating system in C, you'll [[Why_do_I_need_a_Cross_Compiler|need a cross-compiler]]. However, [[GCC_Cross-Compiler|building a GCC cross-compiler]] can be painful. If you just want to write C (not C++), you can use [https://andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html <kbd>zig cc</kbd>] - a C cross-compiler that comes with the [https://ziglang.org/ Zig programming language]. There are [https://ziglang.org/download/ pre-compiled binaries] for Windows, Linux, MacOS, and FreeBSD. |
||
== Bare-Bones with <kbd>zig cc</kbd> == |
|||
This section describes how to build [[Bare Bones]] using <kbd>zig cc</kbd>. First off, install the following software: |
This section describes how to build [[Bare Bones]] using <kbd>zig cc</kbd>. First off, install the following software: |
||
* Zig (tested with version 0.6.0) |
* [https://ziglang.org/download/ Zig] (tested with version 0.6.0) |
||
* a modern gcc toolchain (if <kbd>ld --version</kbd> reports "GNU ld", you |
* a modern gcc toolchain (we just care about the [[LD|linker]] - if <kbd>ld --version</kbd> reports "GNU ld", you're good to go) |
||
* NASM |
* [[NASM]] |
||
Start off with the standard [[Bare Bones]] setup, but use the <kbd>boot.asm</kbd> file provided in [[Bare Bones with NASM]] instructions (since Zig does not come with an assembler, just a C compiler). At that point, you can continue with [[Bare Bones]], but instead of using <kbd>i686-elf-gcc</kbd>, use the following command to build <kbd>kernel.c</kbd>: |
|||
<source lang="bash">zig build-obj --c-source kernel.c -target i386-freestanding</source> |
<source lang="bash">zig build-obj --c-source kernel.c -target i386-freestanding</source> |
||
Line 17: | Line 15: | ||
This should create a <kbd>kernel.o</kbd> file, just the same as if you had used gcc. |
This should create a <kbd>kernel.o</kbd> file, just the same as if you had used gcc. |
||
When you get to the linking step, we won't be able to use <kbd>i686-elf-gcc</kbd>, but luckily, modern versions of GNU <kbd>ld</kbd> are able to emulate almost any other linker, so we can use the system <kbd>ld</kbd> like so: |
When you get to the linking step, we won't be able to use <kbd>i686-elf-gcc</kbd>, but luckily, modern versions of [[LD|GNU <kbd>ld</kbd>]] are able to emulate almost any other linker, so we can use the system <kbd>ld</kbd> like so: |
||
<source lang="bash">ld -m elf_i386 -T linker.ld -o myos.bin boot.o kernel.o</source> |
<source lang="bash">ld -m elf_i386 -T linker.ld -o myos.bin boot.o kernel.o</source> |
Revision as of 22:15, 22 April 2020
If you want to write an operating system in C, you'll need a cross-compiler. However, building a GCC cross-compiler can be painful. If you just want to write C (not C++), you can use zig cc - a C cross-compiler that comes with the Zig programming language. There are pre-compiled binaries for Windows, Linux, MacOS, and FreeBSD.
Bare-Bones with zig cc
This section describes how to build Bare Bones using zig cc. First off, install the following software:
- Zig (tested with version 0.6.0)
- a modern gcc toolchain (we just care about the linker - if ld --version reports "GNU ld", you're good to go)
- NASM
Start off with the standard Bare Bones setup, but use the boot.asm file provided in Bare Bones with NASM instructions (since Zig does not come with an assembler, just a C compiler). At that point, you can continue with Bare Bones, but instead of using i686-elf-gcc, use the following command to build kernel.c:
zig build-obj --c-source kernel.c -target i386-freestanding
This should create a kernel.o file, just the same as if you had used gcc.
When you get to the linking step, we won't be able to use i686-elf-gcc, but luckily, modern versions of GNU ld are able to emulate almost any other linker, so we can use the system ld like so:
ld -m elf_i386 -T linker.ld -o myos.bin boot.o kernel.o
And at that point, you can carry on with Bare Bones, and you should have an operating system, compiled without having to compile your own cross-compiler!