Re: cos() in math.h ?
I was bit by this "bug" in my first real C program. 1986? I looked
HARD. I had to figure it out from the manuals.
With C not including some header files results in a default
prototype being used, int name(...); which may get you linked ok,
but does no checking.
ANSI C is almost C++ The authors compiled all the programs in K&R II
on an early C++ compiler. If you are willing to put up with some
very small differences between the C subset of C++ and gcc's C, you
can avoid this and get considerably more static error checking at
little cost by writing in C and compiling it as C++, with or without
the error checking options below:
g++ -W -Wall --pedantic stuff.c
// no compile/link errors
As someone points out, including the headers does not get you linked
to the libraries. g++ automatically links to most libraries, as well
as doing a bit more error checking.
David Teague, firstname.lastname@example.org
Debian GNU/Linux Because software support is free, timely,
useful, technically accurate, and friendly.
On Thu, 3 Aug 2000, William T Wilson wrote:
> On Thu, 3 Aug 2000, Christophe TROESTLER wrote:
> > simply need to include `math.h'. However, when I compile, I got the
> > error:
> > /tmp/cc9WOsLC.o(.text+0x16): undefined reference to `cos'
> > collect2: ld returned 1 exit status
> This is actually a linker error - undefined references happen when the
> linker (which might be called by the compiler) tries to assemble the
> object files into an executable, but can't find all the function calls
> that the program wants to make. cos() is in the math library, libm.a. So
> you need to add -lm to the command line.
> Including math.h will allow the compiler to compile the object code
> (otherwise you would get warnings or errors about the function declaration
> for cos()) but the actual code that does the computation is in libm.