Re: gcc questions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Nov 27, 2005 at 10:32:16AM +0200, Dotan Cohen wrote:
> > Right -- "m" isn't an option, but actually the name of the library. Well,
> > actually, the library is "libm.so", but the -l assumes a prefix of lib and a
> > suffix of .so.
> Why doesn't it include the math library when I put at the top of the file:
> # include <math.h>

A good question for the instructor of your C class, actually. :)

There's two things going on. First, compiling your code, and second, linking
the resulting binary object files into an executable. Fancy modern compilers
like gcc make that all one step for your convenience.

The first part, compiling, takes your source code and makes a binary file of
it. If you give gcc the "-c" flag, it'll just compile and not try to link:

   gcc -c hellowworld.c

and you'll get "hellowworld.o".

You could then call the linker directly with the command "ld", but it's
easier to use gcc as a front-end again, because it includes a bunch of
default options, so you can conveniently say

   gcc hellowworld.o -o hellowworld

and get your hellowworld executable. But, the default options it gives
includes the core standard C library, but not necessarily any other
libraries, even very standard ones like the math library. So you have to
give the linker the names of those libraries during the link stage:

  gcc hellowworld.o -lm -o hellowworld

There's actually no difference between the -l flag and giving the full path
to the libray directly on the link command line, except that the -l flag
conveniently looks in the standard locations (/lib, /usr/lib, whatever else)
and does the lib- -.so magic.

Turbo C's IDE probably automatically detects the use of these functions and
includes this automatically, but it's doing the same thing behind the
scenes.

Make more sense now?


-- 
Matthew Miller           mattdm@xxxxxxxxxx          <http://mattdm.org/>
Boston University Linux      ------>              <http://linux.bu.edu/>


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux