Find the answer to your Linux question:
Results 1 to 10 of 10
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 ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Location
    Kharagpur, India
    Posts
    2

    Question how to know which function belongs to which library?

    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 this 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..

    can anyone help me?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I am aware of no simple, comprehensive answer to this question.

    I'll give you a simple answer that is not comprehensive. A coder in this situation needs to include -lm on the command line if he wishes to use the math library, or -pthread if he wishes to use POSIX threads. This probably covers most cases.

    Now I'll give you a comprehensive answer that is not simple.

    To find the symbols defined in any given object file or .a library, do this at the command line:
    Code:
    man nm
    Where this gets not-simple is that you should run this on every file that's under /lib or /usr/lib. You can, if you wish, write a shell script that goes through all those files, runs nm on each, filters out everything except the "defined symbols" information, conbines each defined symbol with the file in which it's found, and sort the whole mess by the symbol name.

    I actually did that once; it's fairly straightforward. Maybe someone will step forward and suggest such an animal that's publicly released. I don't know where mine is.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    What I do is man function, and it shows which header it's in. A lot of times from the header you can deduce which library it's in.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Quoth likwid:
    A lot of times from the header you can deduce which library it's in.
    But gibsosmat's main point was that he was having difficulty doing this. I confess that I have the same difficulty, unless I already know the name of the library.

    Could you maybe elaborate a bit on this process of deduction?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just Joined!
    Join Date
    Nov 2007
    Location
    Kharagpur, India
    Posts
    2
    Quote Originally Posted by wje_lf View Post
    I am aware of no simple, comprehensive answer to this question.

    I'll give you a simple answer that is not comprehensive. A coder in this situation needs to include -lm on the command line if he wishes to use the math library, or -pthread if he wishes to use POSIX threads. This probably covers most cases.

    Now I'll give you a comprehensive answer that is not simple.

    To find the symbols defined in any given object file or .a library, do this at the command line:
    Code:
    man nm
    Where this gets not-simple is that you should run this on every file that's under /lib or /usr/lib. You can, if you wish, write a shell script that goes through all those files, runs nm on each, filters out everything except the "defined symbols" information, conbines each defined symbol with the file in which it's found, and sort the whole mess by the symbol name.

    I actually did that once; it's fairly straightforward. Maybe someone will step forward and suggest such an animal that's publicly released. I don't know where mine is.

    Hope this helps.
    hmm..
    I will try that.. from man pages
    but I dont know much about libraries and their structure..
    so I find it difficult to write a shell script for this nm..
    if you have that script or if you can briefly expalin or give me some link that explains basic structure and linking about libs, that will be appreciated..
    I just know little about coding and programming.. but am linux noob..

    and also if I know some function int a lib that is not of the form libxyz.so
    can I still use it for linking?

    Quote Originally Posted by likwid View Post
    What I do is man function, and it shows which header it's in. A lot of times from the header you can deduce which library it's in.
    ya.. thats true.. that is what I did..
    like I tried
    -lsem
    -lsemaphore
    -lpsem
    -lpsemaphore
    etc..
    after that I searched normal file names with sema,sema etc.. lol..
    but nothing turned out..
    pthread.h was clear.. but this specific one is not.. turned out to be some librt.so. I think rt stands for real-time..

    so I felt there might be an elegant way to find it out..

    it is like after all the coding I could not get it running.. lol..

    thanks for your replies

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Code:
    I hope by now you are using the -pthread option in your gcc command so you get the POSIX thread libraries.  That's the standard way of doing it.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    GCC automatically links only the standard C library, libc, for you.
    With the -l option you're telling the linker to link a library with functions not included in the standard C library.
    The -lrt option tells the linker to link the librt.so library, (hence the name "l" stands for "lib" and "rt" for "rt.so") the -lm to libm.so, -lpthread to libpthread.so and so on.
    You can find these libraries or a symlink in the /lib/ or /usr/lib/ directory.

    Go here for an explaination of shared and static libraries:

    An Introduction to GCC - Shared libraries and static libraries

    Regards

  8. #8
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    POSIX threads are a special case. For obscure reasons of portability and flexibility with respect to shared/static libraries (and I don't really understand these reasons), it is generally better to say -pthread instead of -lpthread.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  9. #9
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Well, wje yea it does get complicated in some cases, that you probably encounter a lot more than I do. I am used to doing C programming that is mostly puzzles/math, not so much systems (though I am trying to do it more). I wonder why these things aren't in the manpages, like the headers are. There must be a reason...

  10. #10
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I've long wondered the same thing myself.

    likwid comments on discovering which library defines a given function:
    I wonder why these things aren't in the manpages, like the headers are. There must be a reason.
    Not necessarily. I'm thinking that either:
    1. there's some sort of maintainability issue that keeps a distribution producer from putting this in the man pages; or
    2. it's just tradition.

    I'm guessing (2). The whole idea of man pages goes back a long, long time. To give you perspective, they go back to when Linus Torvalds was less than two years old. I'm guessing that since they're such an ancient institution, nobody wanted to mess with them.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...