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.
Distribution: Red Hat, Fedora, Yellow Dog, Debian, FreeBSD, Embedix
Posts: 106
Rep:
using ld parameters
I'm playing with libtiff. I copied a sample c code that uses
#include <tiffio.h>
from a website and tried to compile it.
ld compained about undefined references to functions defined in tiffio.h.
I checked the path to libtiff, It sits in /usr/lib/ as both .a and .so
I tried compiling with:
gcc test.c -o test -llibtiff -lm
with same result.
Finally, after many tests I figured out that I needed to use -ltiff instead of -llibtiff. What's going on? Why should a library called one thing in the filesystem be referenced by ld under diferent name??? I thought that maybe there's a tiff.a someplace else and that's what got included, so I did ldd test and got /usr/lib/libtiff.so.3 listed. So if the source code references tiffio, the resulting binary is linked to libtiff.so how am I supposed to know that I need to -ltiff?
I'm asking generally about how to know what parameter to use for linking and not just about libtiff in particular.
In general reading the documentation is a good place to start.
From man libtiff:
Code:
INTRO(3T) INTRO(3T)
NAME
libtiff - introduction to libtiff, a library for reading and writ-
ing TIFF files
SYNOPSIS
#include <tiffio.h>
cc file.c -ltiff
DESCRIPTION
libtiff is a library for reading and writing data files encoded
with the Tag Image File format, Revision 6.0 (or revision 5.0 or
revision 4.0). This file format is suitable for archiving multi-
color and monochromatic image data.
...
Suppose I wanted to compile a program that used the fictional library "libsteve". I would include the header file that declares the functions,resources, and anything else inside of libsteve, which Ill call "steve.h".
Presenting fictional program "name.c", which calls "my_name_is()" of libsteve.
now, most libraries have two versions of themselves, usually static and dynamic. libsteve.a, libsteve.so respectively. This command would compile a dynamic executable which would link
to libsteve.so,
Code:
gcc name.c -o name -lsteve
notice I used -lsteve and not -llibsteve, ld assumes the lib prefix, so you only need to call ld with the library name.
Distribution: Red Hat, Fedora, Yellow Dog, Debian, FreeBSD, Embedix
Posts: 106
Original Poster
Rep:
Thanks both of you guys.
jtshaw: I feel stupid. RTFM is the advice I usually give as I've learned that that usually answers the question. Yet, I spent quite a while trying to figure out this problem and never thought to look at the libtiff's man page... I guess I thought the problem was too general to be answered by that particular lib's man. I did check ld's man and didn't see anything in there.
jinksys: very good explanation. This will help me choose right linking params for other libraries in the future. Although calling your example header file and -l parameter the same name might confuse the reader. In my particular case, I include tiffio.h, thus I might think that -ltiffio is the correct option just like steve.h -> -lsteve. Last part of your post clarified that for me.
One more thing: how come with the header file and the library in their default locations gcc doesn't just automagically find the libs needed based on include declarations and tell ld to use them?
GCC has no idea what functions are declared in what libraries. Just because a header is called "moocow.h" doesnt mean it references functions in libmoocow.so, for example. Also, say you wrote your own library, and in that library you wrote your very own custom getch(void) fuction. (getch() is in the ncurses library as well) Now, when you go to compile your application, which getch() will the linker link to? That is why you must tell the linker what libraries to link to.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.