Embedded Handbook/General/Cross-compiling the kernel

== Cross-compiling the kernel == Cross-compile a kernel for a system with flair!

Setup cross-compiling
There are two fundamental variables that the kernel uses to select the target architecture. Normally these values are guessed based on your build environment, but of course that environment here does not match our target embedded system, so we'll need to override them. The variables in question are ARCH and CROSS_COMPILE. The default values for both are found in the top-level Makefile and the values of both may be overridden on the command line.

The ARCH variable is the architecture you're targeting as the kernel knows it. So while portage and other people may use "x86", the kernel uses "i386". Peek in the arch/ subdirectory real quick to figure out what you want to use.

Hopefully the CROSS_COMPILE variable is pretty self-explanatory. Set this to the prefix of your toolchain (including the trailing dash "-"). So if your toolchain is invoked as say, just chop off that trailing and that's what you use:.

There is an additional variable, INSTALL_MOD_PATH, which defines where the directory will be created, and all the modules stored. While you don't have to transfer the kernel sources to your target device, if you build any modules, you'll want this directory.

There are really two ways you can setup the system. You can modify the toplevel or you can override the relevant variables on the command line. How you do it is largely a matter of taste, so we'll cover both. Pick one of the following.

Overriding on the command-line (instead of in the ) would look something like this:

You can use a little helper script if you need to hop between different kernel trees at the same time. We'll call this script :

So now when you want to build a kernel or do anything else, you just execute in place of.

Configure and compile
At this point, configuring and compiling the kernel is like any other kernel, so we won't go into depth as there are plenty of HOWTOs and guides out there which can treat the subject in much greater detail.