PDA

View Full Version : how to know which function belongs to which library?



gibsosmat
11-17-2007, 08:39 PM
as the title states..
what is the best way to know which function call belongs to which library?

I was coding in C and using GCC 4.x..
I know how to use System V IPC semaphores..
but I wanted to learn POSIX semaphores..
so i went through some internet tutorials and from manpages I could learn..
but I have no idea which library to link with my object file,
for my code to run.. I searched and searched.. after a long time I found it..
its like '-lrt'.. (no tute specified which lib to link :()

so my question is in general how can one find the library to reference?
if I am stuck at such point.. man pages dont tell me which lib does this belong to..
they only tell me the header files..

I found some thread 2 years old on a forum asking to grep on /usr/lib/*
I did it and ended up with lot many results saying few matches found..
and one of them is libgc.. I didnt understand why I should be linking if its in libgc..
Also that grep didnt list 'librt' :confused:

can anyone help me?

robwhit
11-17-2007, 08:49 PM
it's in man pthreads. it's -lpthread.

gibsosmat
11-17-2007, 08:59 PM
it's in man pthreads. it's -lpthread.
thanks..
I thought it was only a posix thread lib.. :D
still which is the actual lib for posix IPC functions?

do you have any idea about my original prob?

robwhit
11-17-2007, 10:01 PM
yeah I was wrong. This page seems to agree with you:

http://linux.die.net/man/7/mq_overview

ssharish2005
11-17-2007, 10:56 PM
what is the best way to know which function call belongs to which library?
I tend to get most of the linker flag names from their man page. Most of them tent to have it in their. But some dont.

ssharish

gibsosmat
11-17-2007, 11:35 PM
I tend to get most of the linker flag names from their man page. Most of them tent to have it in their. But some dont.

ssharish
lol..
thats what I was doing :D
but I tried something like
-lpsem
-lposix
-lps
-lsem
-lsema
-lsemaphores
-lmut
-lmutex
etc

but the actual name was -lrt.. I have no clue why it is so..
may be it is meant for ppl who know which lib are you going to use and not which header followed by lib.. :p
still there might be some elegant way to find it out..
else google is for free :cool:.. or Brows LSB..

gibsosmat
11-17-2007, 11:38 PM
yeah I was wrong. This page seems to agree with you:

http://linux.die.net/man/7/mq_overview
no prob.. may be that -lrt is not so famous..
I found on some mailing lists that -lrt got some problem with pthread..
(it was 3-4 years old message)
and was advised to used pthread for semaphores..

and after that found some proff's homepage yelling in red color to compile it with -lrt :D

Salem
11-18-2007, 01:24 AM
If you just do a 'grep' on libraries, you find where the symbols are unresolved as well as defined.

If you want to find where a symbol is defined, then use this approach, then pipe through grep


$ nm --defined-only /usr/lib/lib*.a | more

/usr/lib/libautomode.a:

automode.o:
00000000 b .bss
00000000 d .data
00000000 r .rdata
00000000 t .text
00000000 T _cygwin_premain0
00000000 d pf.0

/usr/lib/libbfd.a:

archive.o:
00000000 b .bss
00000000 d .data
00000000 r .rdata
00000000 t .text
000000a8 r __PRETTY_FUNCTION__.0
000001e0 T __bfd_add_bfd_to_archive_cache
00000000 T __bfd_ar_spacepad

It lists the name of each library and each public symbol which can be found in that library.

gibsosmat
11-18-2007, 07:46 AM
If you just do a 'grep' on libraries, you find where the symbols are unresolved as well as defined.

If you want to find where a symbol is defined, then use this approach, then pipe through grep


$ nm --defined-only /usr/lib/lib*.a | more

/usr/lib/libautomode.a:

automode.o:
00000000 b .bss
00000000 d .data
00000000 r .rdata
00000000 t .text
00000000 T _cygwin_premain0
00000000 d pf.0

/usr/lib/libbfd.a:

archive.o:
00000000 b .bss
00000000 d .data
00000000 r .rdata
00000000 t .text
000000a8 r __PRETTY_FUNCTION__.0
000001e0 T __bfd_add_bfd_to_archive_cache
00000000 T __bfd_ar_spacepad
It lists the name of each library and each public symbol which can be found in that library.
wont this work for .so?