Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Gcc -l option and which libraries to link

Posted on 2003-11-03
7
Medium Priority
?
964 Views
Last Modified: 2013-12-26
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
Comment
Question by:Topace
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Expert Comment

by:rishisk
ID: 9673034
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
 
LVL 24

Accepted Solution

by:
shivsa earned 150 total points
ID: 9673246
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9674150
> 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Expert Comment

by:glassd
ID: 9674228
Often the man page (especially on Linux) will list any libraries which need linking for the given function.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9674310
if in doubt which libs are used automatically by gcc, use:
  gcc -v ...
0
 

Expert Comment

by:rishisk
ID: 9682258
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9682538
ar .. objdump .. ldd .. dbx .. adb .. manymore ..
just to complete the list
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

598 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question