#include <math.h> - problem

#include <math.h> - problem

Post by Steve Summ » Sun, 28 Mar 1999 04:00:00



[I've crossposted this followup -- beware!]


> I'm learning C on a Linux system with gcc 2.7...
> When I try to calculate sin (x) the compiler says:
> undefined reference to 'sin'
> although I wrote
> #include <math.h>
> at the beginning.

Bjoern: you need to use the -lm flag at the end of the command
line when compiling.  You might also want to get ahold of a copy
of the comp.lang.c FAQ list.  It answers this question of yours
(question 14.3), so it just might answer your next question, too.

Linux and gcc folks: I know I've asked this before, but why can't
this be fixed in glibc?  In 1978 on the PDP-11, there might have
been good reason to leave the math functions out of libc.a, but
surely we could afford to place them there today, especially
since the ANSI C standard says they're just as standard as the
rest of the standard library functions.  As it is, gcc+glibc
(and therefore C under Linux) is unnecessarily noncompliant,
unless you argue that "gcc ... -lm" is the standard invocation.

                                        Steve Summit

 
 
 

#include <math.h> - problem

Post by Brian McCaule » Tue, 30 Mar 1999 04:00:00



> [I've crossposted this followup -- beware!]


> > I'm learning C on a Linux system with gcc 2.7...
> > When I try to calculate sin (x) the compiler says:
> > undefined reference to 'sin'
> > although I wrote
> > #include <math.h>
> > at the beginning.

> Bjoern: you need to use the -lm flag at the end of the command
> line when compiling.  You might also want to get ahold of a copy
> of the comp.lang.c FAQ list.  It answers this question of yours
> (question 14.3), so it just might answer your next question, too.

> Linux and gcc folks: I know I've asked this before, but why can't
> this be fixed in glibc?  In 1978 on the PDP-11, there might have
> been good reason to leave the math functions out of libc.a, but
> surely we could afford to place them there today, especially
> since the ANSI C standard says they're just as standard as the
> rest of the standard library functions.  As it is, gcc+glibc
> (and therefore C under Linux) is unnecessarily noncompliant,
> unless you argue that "gcc ... -lm" is the standard invocation.

I agree.  I accept that "gcc ... -lm" *is* the standard but we've
rolled "nls", "socket", "resolv" and so on into "c" why not "m" too?

--

  .  _\\__[oo   faeces from    | Phones: +44 121 471 3789 (home)

 .  l___\\    /~~) /~~[  /   [ | PGP-fp: D7 03 2A 4B D8 3A 05 37...
  # ll  l\\  ~~~~ ~   ~ ~    ~ | http://www.wcl.bham.ac.uk/~bam/
 ###LL  LL\\ (Brian McCauley)  |