This is the mail archive of the crossgcc@sourceware.org mailing list for the crossgcc project.

See the CrossGCC FAQ for lots more information.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: First time user - basic question


Hi Jim,

On Sun, Mar 13, 2011 at 5:53 PM, Jim Norton <jimnorton@jimnorton.org> wrote:
> I read the docs/5 using.txt file. It is really as simple as adding the cross
> compiler bin directory to the path and then setting CROSS_COMPILE env
> variable?

That depends :-)

Many tools which know they might be built with a cross compiler will
come with Makefiles (or whatever they're using to build) which are
already setup with a cross-compiler in mind. I don't think there are
any hard and fast rules, but a general sort of convention which has
been used is to make use of the "CROSS_COMPILE" environment variable.
For example, if you download the Linux kernel tarball, unpack it, and
examine the top-level Makefile you'll find:

    # CROSS_COMPILE specify the prefix used for all executables used
    # during compilation. Only gcc and related bin-utils executables
    # are prefixed with $(CROSS_COMPILE).
    # CROSS_COMPILE can be set on the command line
    # make CROSS_COMPILE=ia64-linux-
    # Alternatively CROSS_COMPILE can be set in the environment.
    # Default value for CROSS_COMPILE is not to prefix executables
    # Note: Some architectures assign CROSS_COMPILE in their arch/*/Makefile
    export KBUILD_BUILDHOST := $(SUBARCH)
    ARCH            ?= $(SUBARCH)
    CROSS_COMPILE   ?=

So if you are building a Linux kernel on an x86 and are targetting the
same machine, simply do a:

$ make menuconfig
$ make bzImage

And the assumptions in Linux's Makefile will handle everything for you.

If, however, you want to build on an x86 machine for a PowerPC target,
the Makefiles are all setup so all you need to change are:

$ make ARCH=powerpc menuconfig
$ export CROSS_COMPILE=powerpc-405-linux-gnu-
$ export PATH=<path to your x-tools>:$PATH
$ make ARCH=powerpc

And your kernel will be built for your PowerPC target.

Notice:
- in my example I'm targetting an AMCC 405 processor (which used to be
a Motorola PowerPC, but is still part of the PowerPC family)
- the "make ARCH=powerpc menuconfig" target doesn't need the cross
compiler, the configuration is done on the host, therefore only the
host compiler is required
- while configuring the kernel, it is up to you to select the correct
CPU to target (i.e. the PowerPC target is a huge family, you need to
make sure your PowerPC-405 compiler is compiling a 405 kernel within
the Linux menuconfig)
- the extra dash at the end of "powerpc-405-linux-gnu-" is not a typo
- if you're interested in the distinction between the host compiler
and the cross-compiler run the build with "make ARCH=powerpc V=1" and
you'll see what exactly is run
- if you want the list of which ARCHs are supported by the Linux
kernel, it is simply the list of directories in your Linux kernel's
"arch" directory

So you see, if you are building a Linux kernel, it is as simple as
defining the CROSS_COMPILE environment variable.

Another common scenario is that what you download uses the autotools
packages (i.e. ./configure; make; make install). In that case the
autotools "magic" can also help you here. In this case the following,
in most cases, will work. In this example I'm cross compiling the "GNU
hello" program for my same powerpc-405 target:

$ export PATH=<path to your powerpc x-tools>:$PATH
$ ~/<path to GNU hello sources>/configure --prefix=<path to your
target filesystem image> --host=powerpc-405-linux-gnu
$ make

If you look at the output from the "configure" and "make" steps you'll
notice it detecting your cross compiler, setting everything up for
cross compiling, and using your cross compiler when building the
application. If you then have a look at the "hello" program that is
built:

$ file src/hello
src/hello: ELF 32-bit MSB executable, PowerPC or cisco 4500, version 1
(SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.31,
with unknown capability 0x41000000 = 0x11676e75, with unknown
capability 0x10000 = 0x90402, not stripped

Notice:
- there was no trailing dash with the "--host" option, this is not a typo

Basically, if you want to cross compile some package you get from the
internet, you'll have to look through its build system (i.e. its
Makefiles) and do whatever you need to make them work. I've provided
examples for two common situations you'll encounter (Linux kernel and
others which use the CROSS_COMPILE environment variable and
autotools). If what you've downloaded uses something else you'll have
to figure that one out on your own. If you're compiling your own code
you can do do whatever you want. For example if all you have is a
simple *.c file you can simply run:

$ export PATH=<path to your x-tools>:$PATH
$ powerpc-405-linux-gnu-gcc -o main main.c

And (hopefully) produce a valid executable for your target:

$ file main
main: ELF 32-bit MSB executable, PowerPC or cisco 4500, version 1
(SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.31,
with unknown capability 0x41000000 = 0x11676e75, with unknown
capability 0x10000 = 0x90402, not stripped

--
For unsubscribe information see http://sourceware.org/lists.html#faq


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]