Complete.Org: Mailing Lists: Archives: linux-help: August 2002:
[linux-help] Re: A c problem
Home

[linux-help] Re: A c problem

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: linux-help@xxxxxxxxx
Subject: [linux-help] Re: A c problem
From: Steven Saner <ssaner@xxxxxxxxxxxxxxx>
Date: Mon, 5 Aug 2002 16:43:13 -0500
Reply-to: linux-help@xxxxxxxxx

On Mon, Aug 05, 2002 at 04:16:09PM -0500, bruce wrote:
> 
> On Monday 05 August 2002 03:33 pm, you wrote:
> > You need to add -lm to the command line. That will tell gcc to link to
> > the math library.
> 
> Hey, that works!  But if I may look ahead a bit, what if I need to add 
> ctypes.h or string.h?  I went back through the man page and found -llib used, 
> but no reference to -lm.  -lmath.h doesn't work.
> 
> Or does -lm add all of the standard headers?
> 
> My question is answered, but I'm still a bit confused.

Part of the confusion is the difference between a header file and a
library. A header file (.h) generally contains macros or definitions
(#define) and/or function prototypes, type definitions, etc. It
generally doesn't include the code that actually implements the
functions. The implementation of the functions are in the libraries in
the form of compiled object code.

So, if you want to use a function that exists in a library, you
generally must include a header file that contains the function
prototype and any definitions, and you must link to the object code in
the library during the compile process.

The linker in gcc automatically links to the standard C library, so
you do not have to explicitly do that. Header files like stdio.h,
string.h, stdlib.h all contain definitions for stuff in the standard C
library. The math library, however, is not automatically linked, and
that is why you need to specify to gcc that you want to link to the
math library (-lm). Including math.h includes the definitions and
prototypes for the math library, but does not actually cause the
library to be linked.

That will cover most, if not all, of the examples in the K&R
book. Basically, if you try to compile something and you get an error
similar to what you were getting, it probably means that you need to
tell gcc to link to some library. The libraries are stored in files
that are named something like libm.* (math library). When you use the
-l option on the gcc command line you skip the "lib" part. That is why
it is just -lm.

I hope that helps.

Steve


> Thanks,
> bruce
> -- This is the linux-help@xxxxxxxxx list.  To unsubscribe,
> visit http://www.complete.org/cgi-bin/listargate-aclug.cgi
-- This is the linux-help@xxxxxxxxx list.  To unsubscribe,
visit http://www.complete.org/cgi-bin/listargate-aclug.cgi


[Prev in Thread] Current Thread [Next in Thread]