How to know the library name when I already know function name
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In all seriousness, I don't know how you would try and use a function (assuming C) in C without knowing the library you are calling it from. If you don't include the library than you can't call the function.
Do you think you could explain a little more clearly what you are trying to do?
It is easy to know which .h file need to include into my source code which calling the local_bh_enable() function. But when I compile, the error occurs (cannot link the library). I need to know the name of library which contains local_bh_enable() to pass to the command line
gcc myproject -lXX
My question is XX = ?
(XX is the name of library which contains local_bh_enable())
The library contains in Linux is in Binary Code, so we cannot search it like the header file, I guess the only way we can do is search through the internet.
-D
--dynamic
Display the dynamic symbols rather than the normal symbols. This
is only meaningful for dynamic objects, such as certain types of
shared libraries.
-- excerpt from man nm
For example:
Code:
#!/bin/sh
# @(#) s1 Demonstrate nm extraction of symbols from shared libraries.
set -o nounset
echo " sh version: $BASH_VERSION"
LIB=/usr/lib/libm.a
echo
echo " Looking at $LIB:"
nm $LIB 2>/dev/null | head -10
LIB=/lib/libm-2.3.2.so
echo
echo " Looking at $LIB:"
nm -D $LIB 2>/dev/null | head -10
exit 1
Which produces:
Code:
% ./s1
sh version: 2.05b.0(1)-release
Looking at /usr/lib/libm.a:
k_standard.o:
U _LIB_VERSION
U __assert_fail
U __copysign
U __errno_location
00000000 T __kernel_standard
U __rint
U fputs
U fwrite
Looking at /lib/libm-2.3.2.so:
00000000 A GLIBC_2.0
00000000 A GLIBC_2.1
00000000 A GLIBC_2.2
w _Jv_RegisterClasses
00021538 D _LIB_VERSION
U __assert_fail
0000c380 T __clog10
00013090 T __clog10f
0001a7b0 T __clog10l
w __cxa_finalize
If I understand your comment regarding the inefficiency of following links, that seems to be an issue of the design of library layout, rather than nm. Please clarify if I have not understood correctly ... cheers, makyo
#!/bin/sh
# @(#) s1 Demonstrate nm extraction of symbols from shared libraries.
set -o nounset
echo " sh version: $BASH_VERSION"
LIB=/usr/lib/libm.a
echo
echo " Looking at $LIB:"
nm $LIB 2>/dev/null | head -10
LIB=/lib/libm-2.3.2.so
echo
echo " Looking at $LIB:"
nm -D $LIB 2>/dev/null | head -10
exit 1
Which produces:
Code:
% ./s1
sh version: 2.05b.0(1)-release
Looking at /usr/lib/libm.a:
k_standard.o:
U _LIB_VERSION
U __assert_fail
U __copysign
U __errno_location
00000000 T __kernel_standard
U __rint
U fputs
U fwrite
Looking at /lib/libm-2.3.2.so:
00000000 A GLIBC_2.0
00000000 A GLIBC_2.1
00000000 A GLIBC_2.2
w _Jv_RegisterClasses
00021538 D _LIB_VERSION
U __assert_fail
0000c380 T __clog10
00013090 T __clog10f
0001a7b0 T __clog10l
w __cxa_finalize
If I understand your comment regarding the inefficiency of following links, that seems to be an issue of the design of library layout, rather than nm. Please clarify if I have not understood correctly ... cheers, makyo
I know exactly what he means though but most people don't seem to get it. In OpenBSD I had written a program that would read ldso hints then run nm on each library and shoot the output into a file in /tmp since it changed a lot anyway. Search in there with something like the mc file viewer and you find lots of treasures. In most cases it included the -l line for the library, but Linux is different, it doesn't work here. You do use it when you don't know what library to link against, or maybe you're looking at an undefined reference error from somebody else's program and trying to fix it. You can find a function name and trace it back to a library. Here, going into a directory full of libraries and doing
Also, you're asking specifically about the Linux kernel's headers, and not C/C++ libraries in general, right? I'm pretty sure the answer, then, actually is RTFM. There are a lot of guides to programming against those.
No, I'm wondering about the libraries in general, not kernel-specific. Here's an example. On a Raspberry Pi I'm trying to isolate the OpenGL ES demo program called hello_triangle and set it up to build with a normal makefile, not be one of 20 or so programs that's set up for cmake. I want to try tinkering with it, making small changes, learning from it. When I run my makefile I get a slew of undefined reference errors like:
Code:
/opt/vc/lib/libEGL.so: undefined reference to `gl11_client_state_init'
/opt/vc/lib/libEGL.so: undefined reference to `glintAttribPointer'
/opt/vc/lib/libEGL.so: undefined reference to `glxx_client_GenFramebuffers'
I'm doing -lGL -lEGL -lbcm_host but -lbcm_host may be the wrong name to use. By nm, grep, etc, some of those symbols are defined in libbrcmEGL.so and libbrcmGLESv2.so among other places, but I need the -l name in order to link it.
I fired up my program (on my OpenBSD laptop) and output looks like:
Code:
search directories: /usr/lib:/usr/X11R6/lib:/usr/local/lib
--------- -lXpm.9.0 /usr/X11R6/lib/libXpm.so.9.0 -----------
t AllocColor
t CreateColors
t CreateXImage
t FreeColors
t GetImagePixels1
t OpenArray
t OpenBuffer
t OpenReadFile
t ParseComment
t PutImagePixels
T fetch_2d_texel_rgba_dxt5
T tx_compress_dxtn
t writedxt5encodedalphablock
t writedxt5encodedalphablock
--------- -lxcb-dri2.1.1 /usr/X11R6/lib/libxcb-dri2.so.1.1 --------- - -----------
a _DYNAMIC
a _GLOBAL_OFFSET_TABLE_
d __dso_handle
d __guard_local
t __i686.get_pc_thunk.bx
T _fini
T _init
t atexit
T xcb_dri2_attach_format_end
T xcb_dri2_attach_format_next
T xcb_dri2_authenticate
T xcb_dri2_authenticate_reply
T xcb_dri2_authenticate_unchecked
T xcb_dri2_connect
T xcb_dri2_connect_alignment_pad
It lists the -l name, the file name and path, and some subset of the symbols. Not all are useful. But a BSD nm has different command-line options, it works differently. There's no quick translation.
No, man local_bh_enable doesn't do anything. And this isn't for headers, -I/opt/vc/include seems to cover those well enough. The Broadcom libraries are in -L/opt/vc/lib but I need names.
By nm, grep, etc, some of those symbols are defined in libbrcmEGL.so and libbrcmGLESv2.so among other places, but I need the -l name in order to link it.
Just drop the "lib" prefix.
These would be "-lbrcmEGL" and "-lbrcmGLESv2"
If that's all you wanted to know, mark the thread as "solved".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.