What is a cross compiler?
A cross compiler is used to create executable for a platform which may be other than the platform on which the compiler is currently running.
To make thing clear:
If I have a machine A(64 bit machine) and I am using a compiler on it for developing OS which will run on machine B(32 bit machine) then the compiler that I am using has to be a cross compiler as it should create executable which will run on machine B.
Why do you need a cross compiler?
If I use a compiler which runs on machine A(64 bit machine) then the executable produced by it may not run on machine B(32 bit machine). Because instruction set for 64 bit machine can be differnt from a 32 bit machine. Hence we need a compiler that understands the instruction set which are compatible with machine B.
How do I build a cross compiler for the OS I am developing?
Steps that must be followed:
1) open your terminal and execute the following commands.
A cross compiler is used to create executable for a platform which may be other than the platform on which the compiler is currently running.
To make thing clear:
If I have a machine A(64 bit machine) and I am using a compiler on it for developing OS which will run on machine B(32 bit machine) then the compiler that I am using has to be a cross compiler as it should create executable which will run on machine B.
Why do you need a cross compiler?
If I use a compiler which runs on machine A(64 bit machine) then the executable produced by it may not run on machine B(32 bit machine). Because instruction set for 64 bit machine can be differnt from a 32 bit machine. Hence we need a compiler that understands the instruction set which are compatible with machine B.
How do I build a cross compiler for the OS I am developing?
Steps that must be followed:
1) open your terminal and execute the following commands.
cd ~ mkdir src2) Download the following files to the "src" folder you just created in the previous step.
- binutils
- when you click on the link you will see a list of files. On that page you have to click on the link which is latest (date modified and version is mentioned there). Make sure you don't download a "patch" or ".sig" (Note that file size is in MB).
- Note extensions to the file names indicate different compression techniques. All of them when uncompressed give the same file. You can download any compression provided both have same version. The only difference will be how much you download.
- gcc
- Its a two step job. First you follow the link. Then in that you click on the link which has latest version. That will lead you to yet another page. There you will get the download link for gcc. Download only one of the file mentioned on that page. Note do not download ".sig" or "diff" files, these files don't have gcc.
- gmp
- mpfr
- click on download and you will find the download link.
- mpc
- click on download and you will find the latest release. Download this latest release.
3) Extract all the files you downloaded in the step 2 to the same folder.
4) Execute the following commands:
cd ~ mkdir opt cd opt mkdir cross export PREFIX="$HOME/opt/cross" export TARGET=i586-elf export PATH="$PREFIX/bin:$PATH" cd $HOME/src mkdir build-binutils cd build-binutils #note that "x.y.z" in the next command is its version number you have to replace it with that. ../binutils-x.y.z/configure --target=$TARGET --prefix="$PREFIX" --disable-nls make make install cd $HOME/src mv gmp-x.y.z gcc-x.y.z/gmp mv mpfr-x.y.z gcc-x.y.z/mpfr mv mpc-x.y.z gcc-x.y.z/mpc mkdir build-gcc cd build-gcc ../gcc-x.y.z/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers make all-gcc make all-target-libgcc make install-gcc make install-target-libgcc
#there is high chance you may run into errors during this installation. Post your queries in the comment section or you can google them (obviously). Some times error can be solved by downloading a different file which has the same version but has a different compression (It happened in my case, dont know why!!!).
#If you don't run into any problem then CONGRATULATIONS your compiler is ready.
#in the next part of this tutorial we will write our first "hello world" OS.
#I recommend you folks to go through this page to understand the details. The explanation is good and most of the information in this tutorial is form that page.
I got this error
ReplyDeleteerrors while building cross compiler
checking for i586-elf-gcc... /home/newspecies/src/build-gcc/./gcc/xgcc -B/home/newspecies/src/build-gcc/./gcc/ -B/home/newspecies/opt/cross/i586-elf/bin/ -B/home/newspecies/opt/cross/i586-elf/lib/ -isystem /home/newspecies/opt/cross/i586-elf/include -isystem /home/newspecies/opt/cross/i586-elf/sys-include
checking for suffix of object files... configure: error: in `/home/newspecies/src/build-gcc/i586-elf/libgcc':
configure: error: cannot compute suffix of object files: cannot compile
See `config.log' for more details.
make: *** [configure-target-libgcc] Error 1
and corrected it by downloading a different file which has the same version but has a different compression