Noeud:Link Editing And Libraries, Noeud « Next »:, Noeud « Previous »:The Assembler, Noeud « Up »:GCC Commands



Link Editing And Libraries

The linking phase is the last phase before producing the executable file. The assembler will have taken the files in assembler and produced object files, the final stage involves linking all of these files together into the final binary.

If there is more than one object file passed to the linker or libraries have been linked, then there may be a number of external symbols to resolve. In fact, even if you pass one source file through gcc, there may be unresolved symbols if you are referencing functions or variables from libraries. An external sybmbol is simply a reference to a variable or function from one file to another file or library. The link/load editor attempts to resolve these references by searching the standard libraries and the object files that have been created as output from the assembler. If there are no unresolved symbols - in other words all references to external symbols were satisfied - an executable file is produced. Unresolved references will mean that the linker will inform you of the references that could not be resolved and no executable will be produced.

During linking, the linker will search all of the standard directories looking for specific libraries. You can include a library by using

-lname

When linking, use library libname.so. If libname.so cannot be found, use libname.a. By default, all libraries contain the prefix lib and the suffix .so or .a; for example -lnsl would look for libnsl.so, the network service layer.

gcc will look in the standard directories looking for this library; if you want to specify a directory to be searched, use the flag

-Ldir

which will tell gcc to also look in directory dir when searching for libraries.

As mentioned previously, shared libraries will be linked first, unless none are found and static libraries are linked (if found). Use

-static

to indicate that static libraries should be linked instead of shared libraries (this option has no effect if the system does not support dynamic linking).

Like all the previous stages you can invoke the linker, ld, directly, instead of relying on gcc (see Passing Arguments to the Assembler and Linker on how to do this from gcc).