gcc library and linker documentation

Torsten Reuss t.reuss@gmx.net
Mon Dec 15 17:10:00 GMT 2003

> Hi
> I am a beginner with gcc and am having difficulty finding certain
> information 
> on programming with gcc, such as:
> -how to search and get relevant documentation for using certain functions

O'Reilly's "Programming with GNU Software" might be good choicd for you. It
handles not only gcc and g++ but also the related tools like gdp and gprof.

That's not an internet tutorial, alright.

> -I'm unclear as to when I would need to use the L option to specify
> libraries 
> on the command line.  I'm used to specifying libraries through #include 
> statements only.

What's generally refered to as libraries, typically consists of two parts:
The header files and the library itself. Header files are what you include
into your C source code by using #include. The library is later either
statically or dynamically linked to your program.

Example 1: You are doing a string comparisn in your C program.
In your source code you will have #include<strings.h> which tells the
compiler that it should read the function declarations in that header file.  So if
you call strcmp somewhere in your program, the compiler will actually know
what you want to do and if you are calling the function correctly (i.e. with
two arguments of type <char *>). The include file is in /usr/include, a
standard path which you don't have to specify. The implementation of strcmp is in
the standard C library (libc). As all C programs are linked against this
library, you don't have to tell the compiler.

So gcc -c -o hello.o hello.c will successfully compile your program
gcc -o hello hello.o will successfully link it.
The resulting program hello will be linked against the standard C library.
If you don't believe me and you are using Linux, BSD or Solaris you can verfy
by issuing the command ldd hello.

Example 2: You have compiled yourself the libz library in your home
directory, so that the include files are now located in /home/joseph/include and the
library is in /home/joseph/lib. You can write your C program just the same
and have the line #include <zlib.h> in your source code. But now you have to
tell the compiler when compiling where to additionally look for include files,
which is the -I option
gcc -c -I/home/joseph/include -o hello.o hello.c will compile it without
Linking is the same, but you need to tell the compiler two things: What
library to additionally link against, and where they are. That's the -l and -L
gcc -L/home/joseph/lib -lz -o hello hello.o

(Note that "z" is the name of the library without the lib and without any .a
or .so suffix).

I hope this will get you further. Regards,


+++ GMX - die erste Adresse für Mail, Message, More +++
Neu: Preissenkung für MMS und FreeMMS! http://www.gmx.net

More information about the Gcc-help mailing list