• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 973
  • Last Modified:

Gcc -l option and which libraries to link

Hey Everyone -

I have a pretty simple question here that I hope to promote maybe some discussion with.  I have been working with threads in an Operating Systems course, and in order to use posix threads with gcc, you need to use the -lpthread option with gcc.  Now, since my instructor informed us of this beforehand, it was no big deal.  However, I went to construct a program using the function sqrt(), from math.h.  I included the header file, and yet I was getting linking errors.  After about a half hour of research, I found that I didn't use the gcc -lm option.  

Herein lies my question -

How does one know that one needs to use a dynamic library such as this?  And once one knows, do you just have to memorize what library to use?  I see this as somewhat of an ambiguity because in the Kernighan & Ritchie book (The C Programming Language, 2nd Ed.) they don't say anything about having to specify a library on the command line.  

Thanks for any help you might be able to give,

~ace
0
Topace
Asked:
Topace
1 Solution
 
rishiskCommented:
Topace,  though I don't have a straight forward answer for this is, you can try using the "ar -t" command for the static libraries that you might be using.

For example, if you want to know what library you have to link for using the sqrt, go to the lib directory where all the standard libs are placed and use ar command for all the .a files. One of the files would give a sqrt.o as output, which means that is the library you need to link during compilation. I know this is kind of lengthy thing. You can probably write a small script to loop through and search for all the .a files and report the name of the file which has the correct one.


0
 
shivsaCommented:
no u may not to remember all the library u have to add but u should know what kind of functions u are using in your program and once u read the man pages for those routine(in this case sqrt()), u will  know what kind of library u need to link at complie time.
u need *.h(header files for definition and library for actual routine.

This is why we use dynamic linking versus static linking.
----------
The link editor, ld(CP) links these object files with each other and with any library functions that you have called in your program. The link editing model you have chosen determines when the library functions are linked:

An archive, or statically linked library, is a collection of object files each of which contains the code for a function or a group of related functions in the library. When you use a library function in your program, a copy of the object file that contains the function is incorporated in your executable at link time.

A static shared library is a file containing object code that several processes may use simultaneously while executing. When a program is link edited with a static shared library, the library code that defines the program's external references is not copied into the program's object file. Instead, a special section called .lib that identifies the library code is created in the object file. When the resulting a.out file is executed, it uses the information in this section to bring the required shared library code into the address space of the process. It should be noted that only one copy of the static shared library code is in memory and is shared by all processes using that library.

A shared object, or dynamically linked library as in our case here, is a single object file that contains the code for every function in the library. When you call a library function in your program and specify a dynamic linking option (-dy) on the cc command line, the entire contents of the dynamically linked library are mapped into the virtual address space of your process at run time. As its name implies, a dynamically linked library contains code that can be used simultaneously by different programs at run time.


0
 
ahoffmannCommented:
> How does one know that one needs to use a dynamic library  ..
'cause you used the functions (like sqrt() in your example)

> And once one knows, do you just have to memorize what library to use?
yes.
Or you write some helper tools, like a Makefile

> .. in Kernighan & Ritchie book .. don't say anything about having to specify a library on the command line.
'cause it is not a C topic to use external libraries

i.g. your question is more about: "how to use compiler and linker to build a executable"
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
glassdCommented:
Often the man page (especially on Linux) will list any libraries which need linking for the given function.
0
 
ahoffmannCommented:
if in doubt which libs are used automatically by gcc, use:
  gcc -v ...
0
 
rishiskCommented:
There is one more command in solaris to try out. It is objdump. The -t option gives out lot of information from which you can find out the function present in the lib. Contrary to the ar command you can use it both for statically and dynamically linked files.

0
 
ahoffmannCommented:
ar .. objdump .. ldd .. dbx .. adb .. manymore ..
just to complete the list
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now