LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to link libraries with g++ (https://www.linuxquestions.org/questions/programming-9/how-to-link-libraries-with-g-545683/)

[KIA]aze 04-13-2007 02:49 AM

How to link libraries with g++
 
I am trying to compile a program using certain libraries.
Among the libraries are ".o", ".lo", ".Plo", "lib*.la", "lib*.so" files organized in several folders.
What is the difference between them and how do I load them with g++?

I read the g++ manual but I didn't really understand the difference between the options -L and -l:
Quote:

LIBRARY_PATH
The value of LIBRARY_PATH is a colon-separated list of directories,
much like PATH. When configured as a native compiler, GCC tries
the directories thus specified when searching for special linker
files, if it can't find them using GCC_EXEC_PREFIX. Linking using
GCC also uses these directories when searching for ordinary
libraries for the -l option (but directories specified with -L come
first).
-llibrary
-l library
Search the library named library when linking. (The second alter-
native with the library as a separate argument is only for POSIX
compliance and is not recommended.)

It makes a difference where in the command you write this option;
the linker searches and 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 directo-
ries 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 fash-
ion. 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.
Is "-l" necessary if I only load libraries not of the form "lib*.a"?

I had no problems including the header files with -I.
The problem are the libraries.

levka 04-13-2007 03:38 AM

When you use option -L you specify the directory where linker can find library files.
When you use option -l you specify the name of library file. It may be different from "lib*.a". If its name is different, just write full name of library file.

For example,

name of the library file is "myownlib.a"
than you specify option like: -lmyownlib.a

if the name of library is "libmyown.a"
than option will be: -lmyown

As for your question about difference between types of library, the lib*.a is a static library. This type of library is added to executive file.
The lib*.so is a shared library, which dynamically linked during executing of program.

Hope it helps you!


All times are GMT -5. The time now is 11:42 AM.