The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Java and Ada, as well as libraries for these languages. The focus here is on the C language, which is the most common programming language for RT Embedded projects and particularly in Linux.
You probably know that, but I will mention it anyway. The output of the C compiler is an object file which is not executable yet. In order to finalize it and create an executable file, it must be linked. The object file might require a function from another object or a library and the linker does all the resolution. In case a function is missing, the output will not link, and the executable will not be created. After a successful link, the default executable name is a.out.
This article is divided to two parts. The first part provides information and examples about:
- Compiling and linking a program.
- Creating a shared library.
- Creating a static library.
The second part provides information and examples about flags which can be specified in order to customize the output (such as; optimization, debug, warnings, etc.).
The basics: Compile and link source and object files
The following example will compile and link a source file. The output file will be a.out.
# gcc mysource.c
In this example, we are specifying a different output file.
# gcc mysource.c –o myprogram
In this example, we only want to generate object files. This is useful in case we are compiling multiple sources which need to be linked together later. Note that it is not linked. There could be unresolved functions.
# gcc –c mysource.c –o myobject.o
Here we link a list of objects to a single executable.
# gcc myobj1.o myobj2.o myobj3.o –o mybigprog
Create a Shared Library
As written in the libraries article, shared libraries must be compiled with Position Independent Code. When we want to link the objects, we need to tell the compiler that we want a share library and not an executable. Library code will not link to an executable because it lacks the main( ) function.
Here is an example how to compile a Position Independent code object:
# gcc –c mysource.c –o myobject.o -fPIC
And in order to create a shared library:
# gcc myobj1.o myobj2.o myobj3.o –o libmybigprog.so -shared
Note that Shared Libraries are not linked, as opposed to executables. They can contain unresolved functions, which reside either in another library, or in the executable who wants to link with it (a callback). Shared libraries have the name notation of libxxxx.so (so stands for Shared Object). By generating the library in this name notation, we could specify easily the name of the library when we’ll want to link with it.
Create a Static Library
Static libraries, unlink shared libraries, are just a function archives (that’s why they are also referenced as archives). They are not a part of the file system. Each executable which requires some of the libraries’ functions will copy their contents to itself. If more than one copy of the same functionality is required (for example, two different programs want the same function) you should consider to create a shared library. It is recommended to compile static libraries with Position Independent Code as well, because a Shared library might require one or more functions from this archive. Therefore, compiling sources to objects is done in the same way as a shared library. In order to create an archive, we specify:
# ar rc libstatic.a myobj1.o myobj2.o myobj3.o # ranlib libstatic.a
Static libraries have the name notation of libxxxx.a (a stands for Archive).
Specifying Compiler and Linker flags
In most cases, we need to configure the compiler with more options, according to our requirements or needs. Please read part 2 of this article to understand and learn how to configure the compiler and linker.
|Check out the ads, there could be something that may interest you there. The ads revenue helps me to pay for the domain and storage.|