Way to view public function names in a library

X

Xiaoxiao

Hi,

I got a C library, is there a way to view the public function names in
this library so that I can use in my C program?

Thanks.
 
F

Flash Gordon

Xiaoxiao wrote, On 09/10/08 18:50:
Hi,

I got a C library, is there a way to view the public function names in
this library so that I can use in my C program?

The best method is to read the documentation. The second best method is
to read the header files provided with the library. If no documentation
or header files are provided then the next best method is to get them,
followed by using a different library.
 
K

Keith Thompson

Xiaoxiao said:
I got a C library, is there a way to view the public function names in
this library so that I can use in my C program?

There is no portable way to do that.

Your best bet is to read the documentation that (presumably)
accompanies the library. Just knowing the names of the functions
doesn't give you enough information.
 
N

Nate Eldredge

Xiaoxiao said:
Hi,

I got a C library, is there a way to view the public function names in
this library so that I can use in my C program?

This is technically off-topic, since it's not about the C language
itself but about whatever compiler and related tools you are using (you
didn't say what they are). You should probably look for another group
that is more specifically dedicated to your system and/or compiler.

However, on many systems the command that does this is called 'nm'.
 
X

Xiaoxiao

There is no portable way to do that.

Your best bet is to read the documentation that (presumably)
accompanies the library.  Just knowing the names of the functions
doesn't give you enough information.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Thanks Keith. My question should be how to know the function prototype
in the shared library. It seems from your answer that there is no way
to do it except getting the document, is that right?
 
X

Xiaoxiao

This is technically off-topic, since it's not about the C language
itself but about whatever compiler and related tools you are using (you
didn't say what they are).  You should probably look for another group
that is more specifically dedicated to your system and/or compiler.

However, on many systems the command that does this is called 'nm'.

Thanks Nate. I didn't know where to ask but because I will call it
from a C function if I can, so I posted the question here. I am going
to use gcc compiler in Linux to compile my C program. I will check
'nm' to see what does it do.
 
K

Keith Thompson

Xiaoxiao said:
Thanks Keith. My question should be how to know the function prototype
in the shared library. It seems from your answer that there is no way
to do it except getting the document, is that right?

That's *probably* right. The format of a shared library, and even
what kind of information is stored in it, is beyond the scope of the C
langauge standard. Typically, though, I think all you could get from
a library is the name and entry point for each function (and the code
that implements each function). Prototypes are generally available in
header files. And if the header file is inconsistent with the
library, you're out of luck.

But even knowing the prototype for each function, though it might tell
you how to write a legal call to the function, isn't going to tell you
how to use it properly. An example from the standard library: knowing
that printf is declared as:

int printf(const char * restrict format, ...);

doesn't tell you what the format string should look like; only the
documentation can tell you that.

If you have a library and you want to call functions in that library,
reading the documentation seems like the obvious answer. The fact
that you're asking for another way to get information about the
functions in a library implies that you have another requirement that
you haven't told us about. If you'll tell us what you're really
trying to do, we may be able to help (or at least offer advice on
where you can find information relevant to your system).
 
X

Xiaoxiao

That's *probably* right.  The format of a shared library, and even
what kind of information is stored in it, is beyond the scope of the C
langauge standard.  Typically, though, I think all you could get from
a library is the name and entry point for each function (and the code
that implements each function).  Prototypes are generally available in
header files.  And if the header file is inconsistent with the
library, you're out of luck.

But even knowing the prototype for each function, though it might tell
you how to write a legal call to the function, isn't going to tell you
how to use it properly.  An example from the standard library: knowing
that printf is declared as:

    int printf(const char * restrict format, ...);

doesn't tell you what the format string should look like; only the
documentation can tell you that.

If you have a library and you want to call functions in that library,
reading the documentation seems like the obvious answer.  The fact
that you're asking for another way to get information about the
functions in a library implies that you have another requirement that
you haven't told us about.  If you'll tell us what you're really
trying to do, we may be able to help (or at least offer advice on
where you can find information relevant to your system).

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Thanks a lot for the help, Keith.
The fact that you're asking for another way to get information about the
functions in a library implies that you have another requirement that
you haven't told us about.

That's true. I got some C source code and shared library from
somewhere and studying it, and will modify it later, and I don't have
the document. I saw somewhere in the original C code that calls the
library functions that I don't know the function prototypes, so that's
why I was asking. Now after reading your post, I also see that there
are header files on the library there, then I can get the function
prototypes from the header files, as you have suggested. But there are
so many of them, and also as you said, even from the function
prototypes I will still need the document to use the functions
correctly.

I have one more question related to this: in generally, does every
shared memory go along with a header file or not? I mean, if I will
create a shared library for someone, do I also need to provide the
header file to him or not? I checked on the internet on the tutorials
of shared libary on Linux, and knew that there were static and dynamic
libraries, but all of them didn't mention whether header files are
needed to provide to others along with the produced libary. Or the
header file is just used for indicting the function prototypes in the
libary and doesn't really need for the application which calls the
library?

please forgive my entry level question.

Thank you so much for your patience and help again.
 
N

Nate Eldredge

Xiaoxiao said:
I have one more question related to this: in generally, does every
shared memory go along with a header file or not? I mean, if I will
create a shared library for someone, do I also need to provide the
header file to him or not?

Generally, he will need the header file if he wants to compile a program
that uses the library. If you give him a program that's already
compiled, together with the shared library, the header file is not needed.
 
X

Xiaoxiao

Generally, he will need the header file if he wants to compile a program
that uses the library.  If you give him a program that's already
compiled, together with the shared library, the header file is not needed..

Thanks a lot for the clarification, Nate. Now I understand them much
better.
 
A

Antoninus Twink

However, on many systems the command that does this is called 'nm'.

As "Keith Thomson" has pointed out (in an unusually helpful answer for
him, given that the subject is one that he mistakenly regards is "off
topic"), while nm will list the *names* of the symbols (like function
names) in an object file or library, it won't tell you the actual
prototype of a function - you need the header file for that.

You should also be aware that if the library has been stripped of
debugging symbols (this will usually be the case for release versions of
libraries), then nm won't be able to list the symbols because they'll no
longer be there.
 
N

Nate Eldredge

Antoninus Twink said:
As "Keith Thomson" has pointed out (in an unusually helpful answer for
him, given that the subject is one that he mistakenly regards is "off
topic"), while nm will list the *names* of the symbols (like function
names) in an object file or library, it won't tell you the actual
prototype of a function - you need the header file for that.
True.

You should also be aware that if the library has been stripped of
debugging symbols (this will usually be the case for release versions of
libraries), then nm won't be able to list the symbols because they'll no
longer be there.

That's not correct. *Debugging* symbols may be missing, but what's
needed here are the symbols for the library's functions and variables
themselves. These must be present, because otherwise it would be
impossible to link against the library.

In the case of a shared library, some versions of nm don't show dynamic
symbols by default, so it may appear that there are no symbols. My
version of nm uses the -D option to show dynamic symbols, which would
still be present.

You are probably thinking of binaries (executables), which often are
distributed entirely stripped of symbols, as they're no longer needed.
 
C

CBFalconer

Xiaoxiao said:
I got a C library, is there a way to view the public function
names in this library so that I can use in my C program?

I suggest reading the documentation.
 
X

Xiaoxiao

I suggest reading the documentation.

Thanks a lot for more help again. I really appreciate the Internet and
everyone's unselfish help. When I know something in the future, I will
also try to help people on their questions.
 
A

Antoninus Twink

Thanks a lot for more help again. I really appreciate the Internet and
everyone's unselfish help.

Yep - help like that provided by CBF above you won't get anywhere except
on the internet...
 
C

CBFalconer

Xiaoxiao said:
Thanks a lot for more help again. I really appreciate the Internet
and everyone's unselfish help. When I know something in the future,
I will also try to help people on their questions.

Bad idea. If you send them to a suitable newsgroup, you can answer
their question there. You will have some assurance that erroneous
answers will be corrected by other users. This (correction) does
not necessarily apply without picking the correct group.

Also, please always delete the signature from any quotation in your
reply. The signature is everything following the '-- ' sig marker
(inclusive).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top