Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The -l option in gcc

Tags:

gcc

I have just began reading the book: Advanced Programming in Unix Environment and try to compile the first example code, just the same as in this thread. Although the problem for the compilation is solved using the command:


gcc -o myls myls.c -I SCADDRESS/include/ -L SCADDRESS/lib/ -lapue

I looked it up in the GCC manual and can't find what does the gcc option -lxxx mean, where xxx stands for the base name of a header file(in this case, it's apue.h). According to the manual, xxx should be some library files, either end with .so for shared object files, or with .a for static libraries.

Can anyone explain it a little bit? Thx in advance:)

like image 438
Shihao Xu Avatar asked Oct 04 '15 00:10

Shihao Xu


People also ask

What does option in GCC do?

When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options" allow you to stop this process at an intermediate stage. For example, the -c option says not to run the linker.

What is option in compiler?

A compiler option (also known as a switch) is an optional string of one or more alphanumeric characters preceded by a dash (-) (Linux* and Mac OS*) or a forward slash ( / ) (Windows*). Some options are on by default when you invoke the compiler.

What is Wall option in GCC?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

What is fPIC in GCC?

fPIC option in GCC enables the address of shared libraries to be relative so that the executable is independent of the position of libraries. This enables one to share built library which has dependencies on other shared libraries. fPIC stands for "force Position Independent Code".


2 Answers

This is documented in §2.13 "Options for Linking" in the GCC manual:

-llibrary

Search the library named library when linking.

It makes a difference where in the command you write this option; the linker searches processes libraries and object files in the order they are specified. Thus, `foo.o -lz bar.o' searches library `z' after file `foo.o' but before `bar.o'. If `bar.o' refers to functions in `z', those functions may not be loaded.

The linker searches a standard list of directories for the library, which is actually a file named `liblibrary.a'. The linker then uses this file as if it had been specified precisely by name.

The directories searched include several standard system directories plus any that you specify with `-L'.

Normally the files found this way are library files--archive files whose members are object files. The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion. The only difference between using an `-l' option and specifying a file name is that `-l' surrounds library with `lib' and `.a' and searches several directories.

like image 163
ruakh Avatar answered Oct 05 '22 23:10

ruakh


The -l option tells gcc to link in the specified library. In this case the library is apue, that it happens to line up with the name of a header file is just how the apue coders designed their project. In reality the -l option has nothing to do with header files. Like cas says in the comments, read the manpage, it'll give you much more information.

like image 38
Michael Albers Avatar answered Oct 06 '22 00:10

Michael Albers