LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2004, 05:01 PM   #1
artur
Member
 
Registered: Apr 2002
Location: Illinois, US
Distribution: Red Hat, Fedora, Yellow Dog, Debian, FreeBSD, Embedix
Posts: 106

Rep: Reputation: 15
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.
 
Old 02-12-2004, 05:28 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
...
 
Old 02-12-2004, 08:37 PM   #3
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
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!
 
Old 02-13-2004, 10:01 AM   #4
artur
Member
 
Registered: Apr 2002
Location: Illinois, US
Distribution: Red Hat, Fedora, Yellow Dog, Debian, FreeBSD, Embedix
Posts: 106

Original Poster
Rep: Reputation: 15
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?
 
Old 02-13-2004, 02:27 PM   #5
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
many parameters... os2 Programming 3 07-30-2005 04:56 PM
DDD parameters`? jnusa Linux - Software 0 12-06-2004 02:52 AM
Nautilus Parameters hydorn Linux - Software 0 07-01-2004 10:07 AM
Cdrecord parameters. glore Linux - Newbie 2 05-15-2004 09:06 AM
pthread_create parameters AquamaN Programming 3 03-16-2004 10:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration