Nwcc

From OSDev Wiki
Jump to: navigation, search

Contents

Nils Weller's C Compiler

nwcc is a small C compiler written by Nils Weller in his spare time. It can output gas, yasm, and nasm assembly output, and supports several architectures. Currently, it is not yet complete, and is missing several features including some that may be needed by osdev. (ie. bitfields) However, it does work and can compile itself. Moreover, it assembles to straight assembly; possibly giving the osdever more control over how his code is created. Note, however, that nwcc currently has not optimizations and therefore will not produce code as esteemed as GCC.

Using With Osdev

Using nwcc for osdev is surprisingly easy. First, you have to compile your C sources like so:

nwcc -arch=x86 kernel.c -nostdinc

This instructs nwcc to cross-compile kernel.c to the x86 architecture and not to include any standard includes. (Although, the source seems identical without -nostdinc, but just to be safe.) Now, an important point here. When you instruct nwcc to cross-compile something, it does not invoke an assembler, so you are free to assemble yourself with whatever assembler nwcc is currently set to. (This can be adjusted with the -asm=x flag.) Being as such, this command will output kernel.asm; just waiting to be assembled.

yasm -f elf kernel.asm -o kernel.o

Note: On my system. (An x86-64 Linux box.) nwcc only supports yasm and gas. So you may want to adjust some stuff here if you are using nasm.

Yasm takes nwcc's output and assembles it to an object file. Not much to be said here.

ld -m elf_i386 -T kernel.ld k_entry.o kernel.o

Note: k_entry.o is assumed to be a kernel entry from your boot loader. Also, omit '-m elf_i386' if not using a 64bit GCC

This then assembles and links your kernel according to your LD linker script and entry code.

That's it?

Yeah, that's it. nwcc is looking to be a promising C compiler, without all of the bloat. **cough**GCC**cough** If you can get past some of its more unique features, you should be able to use it to make a small, clean os.

See Also

External Links

Personal tools
Namespaces
Variants
Actions
Navigation
About
Toolbox