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
LVL 1
TopaceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.