LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   using ld parameters (https://www.linuxquestions.org/questions/programming-9/using-ld-parameters-145417/)

artur 02-12-2004 05:01 PM

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.

jtshaw 02-12-2004 05:28 PM

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


jinksys 02-12-2004 08:37 PM

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>

void main()
{
my_name_is();
return 0;
}

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.

Hope this helps!

artur 02-13-2004 10:01 AM

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?

jinksys 02-13-2004 02:27 PM

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.