gcc questions

Ian Malone ibm21 at cam.ac.uk
Sun Nov 27 10:12:54 UTC 2005


Dotan Cohen wrote:

 > Matthew Miller <mattdm at mattdm.org> wrote:

 >> On Sun, Nov 27, 2005 at 12:57:29AM +0200, Dotan Cohen wrote:
 >
 >>>> gcc -lm 1_3.c
 >>
 >>> Thank you, it compiled now. I just spacebar'ed about 100 times in
 >>> the gcc man page looking for the "m" option that you mention. I did
 >>> find the "l" option (search the library named library when linking),
 >>> but not the "m" option.
 >

1. $ info gcc
Info documentation is useful for complex programs like gcc and bash,
far easier to navigate than a man page (but much less ubiquitous).

2. The entry "-l LIBRARY" (info) or "-l library" bash tells you that
whatever comes after "-l" is a library name.  The space is not 
necessary, so as Matthew Miller points out:

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

There are two separate things going on:

#include <math.h>

Includes declarations for the functions in the library, it is not the
library itself.  It is equivalent to putting declarations like:

double sqrt(double);

at the top of you source file (except you /should/ use the include,
because it will get them right), telling the compiler you are going
to us some functions, they look like this, but you're not putting them
in this file.

The library itself contains the compiled function bodies, the
double sqrt(double squared) {
   double root;
   /* do clever things to get root = square root of squared
    * ... */
   return root;
}

The -lm line is similar to adding a compiled object file to the
gcc invocation, eg.

$ gcc uses_maths.c my_maths.o  -o uses_maths



-- 
imalone




More information about the fedora-list mailing list