mirror of
				https://github.com/cfenollosa/os-tutorial.git
				synced 2025-06-13 12:54:24 +00:00 
			
		
		
		
	lesson 11, cross-compiler
This commit is contained in:
		
							parent
							
								
									b9999306a5
								
							
						
					
					
						commit
						32355dbc28
					
				
							
								
								
									
										76
									
								
								11-kernel-crosscompiler/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								11-kernel-crosscompiler/README.md
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,76 @@
 | 
			
		||||
*Concepts you may want to Google beforehand: cross-compiler*
 | 
			
		||||
 | 
			
		||||
**Goal: Create a development environment to build your kernel**
 | 
			
		||||
 | 
			
		||||
If you're using a Mac, you will need to do this process right away. Otherwise, it could have waited
 | 
			
		||||
for a few more lessons. Anyway, you will need a cross-compiler one we jump to developing in a higher
 | 
			
		||||
language, that is, C. [Read why](http://wiki.osdev.org/Why_do_I_need_a_Cross_Compiler%3F)
 | 
			
		||||
 | 
			
		||||
I'll be adapting the instructions [at the OSDev wiki](http://wiki.osdev.org/GCC_Cross-Compiler). 
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Required packages
 | 
			
		||||
-----------------
 | 
			
		||||
 | 
			
		||||
First, install the required packages. On linux, use your package distribution. On a Mac, [install brew](http://brew.sh/) if
 | 
			
		||||
you didn't do it on lesson 00, and get those packages with `brew install`
 | 
			
		||||
 | 
			
		||||
- gmp
 | 
			
		||||
- mpfr
 | 
			
		||||
- libmpc
 | 
			
		||||
- gcc
 | 
			
		||||
 | 
			
		||||
Yes, we will need `gcc` to build our cross-compiled `gcc`, especially on a Mac where gcc has been deprecated for `clang`
 | 
			
		||||
 | 
			
		||||
Once installed, find where your packaged gcc is (remember, not clang) and export it. For example:
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
export CC=/usr/local/bin/gcc-4.9
 | 
			
		||||
export LD=/usr/local/bin/gcc-4.9
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
We will need to build binutils and a cross-compiled gcc, and we will put them into `/usr/local/i386elfgcc`, so
 | 
			
		||||
let's export some paths now. Feel free to change them to your liking.
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
export PREFIX="/usr/local/i386elfgcc"
 | 
			
		||||
export TARGET=i386-elf
 | 
			
		||||
export PATH="$PREFIX/bin:$PATH"
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
binutils
 | 
			
		||||
--------
 | 
			
		||||
 | 
			
		||||
Rember: always be careful before pasting walls of text from the internet. I recommend copying line by line.
 | 
			
		||||
 | 
			
		||||
```sh
 | 
			
		||||
mkdir /tmp/src
 | 
			
		||||
cd /tmp/src
 | 
			
		||||
curl -O http://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.gz # If the link 404's, look for a more recent version
 | 
			
		||||
tar xf binutils-2.24.tar.gz
 | 
			
		||||
mkdir binutils-build
 | 
			
		||||
cd binutils-build
 | 
			
		||||
../binutils-2.24/configure --target=$TARGET --enable-interwork --enable-multilib --disable-nls --disable-werror --prefix=$PREFIX 2>&1 | tee configure.log
 | 
			
		||||
make all install 2>&1 | tee make.log
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
gcc
 | 
			
		||||
---
 | 
			
		||||
```sh
 | 
			
		||||
cd /tmp/src
 | 
			
		||||
curl -O http://mirror.bbln.org/gcc/releases/gcc-4.9.1/gcc-4.9.1.tar.bz2
 | 
			
		||||
tar xf gcc-4.9.1.tar.bz2
 | 
			
		||||
mkdir gcc-build
 | 
			
		||||
cd gcc-build
 | 
			
		||||
../gcc-4.9.1/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --disable-libssp --enable-languages=c --without-headers
 | 
			
		||||
make all-gcc 
 | 
			
		||||
make all-target-libgcc 
 | 
			
		||||
make install-gcc 
 | 
			
		||||
make install-target-libgcc 
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
That's it! You should have all the GNU binutils and the compiler at `/usr/local/i386elfgcc/bin`, prefixed by `i386-elf-` to avoid
 | 
			
		||||
collisions with your system's compiler and binutils.
 | 
			
		||||
 | 
			
		||||
You may want to add the `$PATH` to your `.bashrc`. From now on, on this tutorial, we will explicitly use the prefixes when using
 | 
			
		||||
the cross-compiled gcc.
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user