Solved

Gcc -l option and which libraries to link

Posted on 2003-11-03
7
932 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
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 50 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
not able to insert into temp table 68 150
if loop in java 3 114
fizzArray  challenge 1 47
matchUp  challenge 6 48
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

707 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now