views:

67

answers:

3

What's .a files in C programming in linux ? Is it library file ?

To merge with the math library libm.a you would type

 cc -o program_name prog.c -lm

when you compile the program. The -lm means: add in libm. If we wanted to add in the socket library libsocket.a to do some network programming as well, we would type

 cc -o program_name prog.c -lm -lsocket

and so on. 

Here how compiler find that -lm is pointing to the file libm.a , and -lsocket as libsocket.a ?

And if we add the header file to the program, Is it must we want to mention library while compiling ?

+1  A: 

.a files are static libraries, as opposed to .so files which are dynamic libraries. Normally gcc looks for dynamic libraries if available unless passed -static.

The header contains definitions that the compiler needs in order to build the source code into an object file, but the libraries contain the actual routines that the linker needs to turn the object file into an executable.

Ignacio Vazquez-Abrams
+4  A: 

As Ignacio says, .a files are static libraries. The "a" stands for "archive" and .a files are built by a program named "ar".

Each .a file contains one or more .o files and an index of names. During the link process only the .o files that contain used names are included into the final program. This is so that instead of including the entire C library, only used functions like "printf" are copied.

How does the compiler find the libraries? It has a built-in collection of library paths that are searched. As an example, GCC will tell you its search paths if asked:

# gcc -print-search-dirs
install: /usr/lib/gcc/i686-redhat-linux/4.4.4/
programs: =/usr/libexec/gcc/i686-redhat-linux/4.4.4/:/usr/libexec/gcc/i686-redhat-linux/4.4.4/:/usr/libexec/gcc/i686-redhat-linux/:/usr/lib/gcc/i686-redhat-linux/4.4.4/:/usr/lib/gcc/i686-redhat-linux/:/usr/libexec/gcc/i686-redhat-linux/4.4.4/:/usr/libexec/gcc/i686-redhat-linux/:/usr/lib/gcc/i686-redhat-linux/4.4.4/:/usr/lib/gcc/i686-redhat-linux/:/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../../i686-redhat-linux/bin/i686-redhat-linux/4.4.4/:/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../../i686-redhat-linux/bin/
libraries: =/usr/lib/gcc/i686-redhat-linux/4.4.4/:/usr/lib/gcc/i686-redhat-linux/4.4.4/:/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../../i686-redhat-linux/lib/i686-redhat-linux/4.4.4/:/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../../i686-redhat-linux/lib/:/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../i686-redhat-linux/4.4.4/:/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../:/lib/i686-redhat-linux/4.4.4/:/lib/:/usr/lib/i686-redhat-linux/4.4.4/:/usr/lib/

You can add more library search paths by using the "-L /path" option.

In those paths, it first searches for "dynamic libraries" which are named with a ".so" extension. It then searches for static libraries with a ".a" extension. It always adds "lib" to the front of the name.

Zan Lynx
A: 

The compiler "knows" to look for libm.a (or libm.so) when you pass it the option -lm, because that's how the -l option is documented and implemented: Take the characters following -l (here just m), prefix lib and suffix .a to get libm.a

Each library can have its own relation between header and library files used. It's uncommon for a header file to require no library at all, but it's more common for a library to have multiple header files.

MSalters