Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-13-2007, 02:49 AM   #1
Registered: Jun 2006
Distribution: Debian, Ubuntu, Windows XP
Posts: 146

Rep: Reputation: 16
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:
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
-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.
Old 04-13-2007, 03:38 AM   #2
LQ Newbie
Registered: Apr 2007
Location: Ukraine
Distribution: Red Hat, Debian
Posts: 4

Rep: Reputation: 0
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!


g++, library

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
link non-default libraries in KDevelop?? TLV Programming 3 12-14-2004 11:25 AM
Wake on Lan - Link on b4 power up, No Link light after shutdown? Mark007 Linux - Networking 0 11-20-2004 08:16 PM
Dynamic link libraries eshwar_ind Programming 1 05-09-2004 11:02 PM
Linux Version of Dynamic Link Libraries paul_eniki Programming 4 02-05-2004 09:25 AM
gaim trying to link static and shared libraries rose_bud4201 Linux - Software 0 09-28-2003 12:27 AM > Forums > Non-*NIX Forums > Programming

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration