9

I have just begun reading the book Advanced Programming in Unix Environment and try to compile the first example code, just the same as in this question.

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, but 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shihao Xu
  • 721
  • 1
  • 7
  • 14
  • 1
    `man gcc`, search for `-llibrary`. – cas Oct 04 '15 at 00:34
  • 1
    @cas The gcc manpage does not describe the behavior for the option `-lheaderFile`. It's all about .so and .a libraries. The compiler can't find the header files and the implementations(.c and/or .o), if the programmer don't specify the include/library path explicitly. I just have no idea, how the compiler do these things behind the scenes. – Shihao Xu Oct 04 '15 at 21:16
  • 1
    There is no such option. `-l` (lowercase L) is for specifying libraries, not header files. perhaps you're thinking of `-I` (Capital I) for specifying directories to search in for header files. – cas Oct 04 '15 at 21:28
  • @cas I didn't notice that a file named libapue.a resides in the lib directory. THX : ) – Shihao Xu Oct 04 '15 at 21:41
  • Related: *[Why do you have to link the math library in C?](https://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c)* – Peter Mortensen Oct 27 '22 at 23:40

2 Answers2

11

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.

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • 1
    I think linking a header file is something different to linking in a .o/.a/.so file. After all, .h file have no implementation at all. – Shihao Xu Oct 04 '15 at 21:20
  • 2
    @ShihaoXu: Correct: there's no such thing as "linking a header file". Header files are included using the preprocessor's `#include` directive. In your example, *Advanced Programming in the UNIX Environment* provides *both* a header file named `apue.h` that you can `#include` so that the symbol declarations are available to your code, *and* a library called `apue.o` or `apue.a` or `apue.so` that you can `-l` so that the actual symbol definitions get linked into your program. (This is a pretty common pattern.) – ruakh Oct 04 '15 at 21:26
  • 1
    AHA! I found a file named libapue.a in the library directory provided along with the book. The compile example in the book does not mention it at all...and i just recognize the -lapue as "linking the header file apue.h" by mistake. THX! – Shihao Xu Oct 04 '15 at 21:37
  • @ShihaoXu: You're welcome! And yes, you're right -- the documentation specifies that it should be `libapue.a`; I don't know how I missed that, but I'm glad you figured it out on your own. :-) – ruakh Oct 05 '15 at 00:56
2

The -l option tells GCC to link in the specified library. In this case, the library is apue, and 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 man page; it'll give you much more information.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michael Albers
  • 3,721
  • 3
  • 21
  • 32
  • It does make some sense for me. Do you know exactly how the compiler know the directories to find and link the include files and libs? It doesn't work even if i specify the relative/absolute path of the header files in each source file. – Shihao Xu Oct 04 '15 at 21:26
  • 1
    The compiler has a few standard places to look for header files, like /usr/include and a few standard places to look for libraries, like /usr/lib. You can add directories to search for header files by using the -I option and add library search directories by using the -L option. – Michael Albers Oct 04 '15 at 21:43
  • Now i know how to use "the most important three options (-l -L -I) for gcc" from my point of view. : ) THX for answering. – Shihao Xu Oct 04 '15 at 21:49