i386-aros-gcc Compile Options

I tried to research the compile options for the command in my last post. I did not have much luck finding information on it. So I asked thy master, Rob Norris. He explained it extremely well. I wanted to post it here in-case others needed help and to keep a record of it.

I’ll try and explain here. “man gcc” and “man ld” of course have all the info, but are pretty heavy going.

A breakdown:

– i386-aros-gcc: this is running the compiler proper. You need to use a special compiler that knows the specifics of AROS; how its headers and shared libraries work, what features it supports, etc. This is why using the regular “gcc” supplied with your system won’t work for AROS programs.

This is also known as a “cross compiler”, since you’re compiling programs destined for one system (AROS) on a system of a different type (Linux).

– -o hello: specifies the output file, you probably got that.

– -D __AROS__: this predefines a macro and is exactly equivalent to writing ‘#define __AROS__’ someone in the C source file. This is an OS-identifying macro that the SDK cross compiler should be providing itself, which is what mazze and I have been talking about on the aros-dev. There’s a bug in the SDK compiler if it doesn’t provide this, you shouldn’t have had to.

I’ll try to find time over the next week to see about a fix, but frankly the SDK has a lot more wrong with it than just this, which is why I’m making a new development kit.

– hello.c: obviously, specifies the source file.

– -lmui. tells the compiler it should look for any missing functions in a library file called libmui.a. Read on…

Compilation happens in two stages. The compiler takes your C source files, and generates what are known as object files from them. Then a second process known as the linker runs and pulls all the object files and all the libraries you specify using -l together into one file.

As it does this, it resolves references between them. For example, hello.c (via some header file magic you don’t see) tries to call functions called MUI_MakeObject() and MUI_NewObject(). These functions
don’t exist inside hello.c, so when the compiler generates the object file for hello.c (usually hello.o), it will contain information that says “this program will need these functions that I don’t know about”.

This object file is not a program, and cannot be run. If you try to make it into a program without linking it with something that does provide those functions, it won’t know what to do and will tell you about it, eg:

$ i386-aros-gcc -D__AROS__ -o hello hello.c
There are undefined symbols in ‘hello’:
U MUI_MakeObject
U MUI_NewObject

Specifying -lmui is exactly the same as specifying the full path to libmui.a when linking, eg:

$ i386-aros-gcc -D__AROS__ -o hello hello.c /usr/local/i386-pc-aros/lib/libmui.a

Using -l will make the compiler search for it in a number of places.
Your usual compiler will look in /lib, /usr/lib, /usr/local/lib, etc.

The only other thing to understand about this is that the command you’re using will generate the hello.c object file internally, as its trying to produce an executable. You can specify multiple source files, eg:

$ i386-aros-gcc -D__AROS__ -o hello hello.c options.c something.c -lmui

When you do that, it tries to compile all three source files every time, which is wasteful if your project contains many source files. If you only made a change in one of them, then you only want to compile a single file. A better way is to do it with a sequence of commands:

$ i386-aros-gcc -D__AROS__ -c hello.c
$ i386-aros-gcc -D__AROS__ -c options.c
$ i386-aros-gcc -D__AROS__ -c something.c
$ i386-aros-gcc -o hello hello.o options.o something.o -lmui

This demonstrates the split between the compiler and linker. -c tells gcc to invoke the compiler only and produce an object file. You’ll notice that you get a hello.o on your disk after this command. The last command will invoke the linker only, as there are no C files listed.

Later, if you make a change in hello.c, you only need to recompile that file and relink:

$ i386-aros-gcc -D__AROS__ -c hello.c
$ i386-aros-gcc -o hello hello.o options.o something.o -lmui

So its faster, but more work when you’re typing. However by this time you won’t be typing these commands out every time, you’ll be using the make program and a Makefile to automate this for you.

2 comments so far

  1. craid-hjb on

    Hey Nova, it´s really nice to see you chosen C as the path to follow, I think its the best one besides of LUA for some smaller stuff. Might be also a good oportumity for newcomers to follow your blog. Good luck mate!

  2. novacode on

    Thanks craid! I’m devoted to learning it and contributing.
    I’m glad you think it would be good for beginners. It will be nice if my blog helps.


Leave a reply