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) |
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. Code:
#include <steve.h> to libsteve.so, Code:
gcc name.c -o name -lsteve Hope this helps! |
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.
|
All times are GMT -5. The time now is 01:39 PM. |