Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 11 Apr 2006 23:27:52 +0000
From:      "Jonathan Herriott" <herriojr@gmail.com>
To:        "Nicolas Blais" <nb_root@videotron.ca>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: pow function working unexpectedly
Message-ID:  <6a56d69c0604111627x78760975g8a8112c0d90f9ea@mail.gmail.com>
In-Reply-To: <200604111912.44611.nb_root@videotron.ca>
References:  <6a56d69c0604111554o587ce2c5ha1ff4ea20bbab0a4@mail.gmail.com> <200604111857.43171.nb_root@videotron.ca> <6a56d69c0604111607l5fba5939pfc6461a99a2ceab3@mail.gmail.com> <200604111912.44611.nb_root@videotron.ca>

next in thread | previous in thread | raw e-mail | index | archive | help
With reference to my first example given is what I was talking about.=20
The first example properly ran with just compiling with:

gcc test.c

Here's the first example again:
> > > > #include <stdio.h>
> > > > #include <math.h>
> > > >
> > > > int main()
> > > > {
> > > >      printf("%f\n", pow(2,3));
> > > >      return 0;
> > > > }

Thanks,
Jon

On 4/11/06, Nicolas Blais <nb_root@videotron.ca> wrote:
> On Tuesday 11 April 2006 19:07, Jonathan Herriott wrote:
> > Question then.  Why would it work when I don't use variables if I
> > don't include the library?  This question is just out of curiosity.
> >
> > On 4/11/06, Nicolas Blais <nb_root@videotron.ca> wrote:
> > > On Tuesday 11 April 2006 18:54, Jonathan Herriott wrote:
> > > > I just want to make sure I'm not being stupid before I send in a bu=
g.
> > > > The problem seems to be with gcc when using the pow function in the
> > > > math.h library.  Here's code that works:
> > > >
> > > > #include <stdio.h>
> > > > #include <math.h>
> > > >
> > > > int main()
> > > > {
> > > >      printf("%f\n", pow(2,3));
> > > >      return 0;
> > > > }
> > > >
> > > >
> > > > Now, the following will not compile:
> > > >
> > > > #include <stdio.h>
> > > > #include <math.h>
> > > >
> > > > int main()
> > > > {
> > > >      int x =3D 2; // I tried these as doubles too since the pow fun=
ction
> > > > takes doubles
> > > >      int y =3D 3;
> > > >
> > > >      printf("%f\n", pow(x,y));
> > > >      return 0;
> > > > }
> > > >
> > > > I compiled both programs using:
> > > > gcc test.c
> > > >
> > > > The second example gives the following error:
> > > >
> > > > /var/tmp//ccxtkMwv.o(.text+0x45): In function `main':
> > > > : undefined reference to `pow'
> > > >
> > > > If I comile with g++, I have no issues.  Are these results that I
> > > > should have?  If so, why?  If not, I'm going to submit the bug on g=
cc
> > > > (or the linker, but I'm guessing it's the same group).
> > > >
> > > > Thanks,
> > > > Jonathan Herriott
> > >
> > > gcc -lm test.c works. You forgot to include the math library.
> > >
>
> I'm not sure I get your question, but if your not using the math library
> (from
> variables or functions), then the compiler could care less if it was link=
ed
> with the math library or not.
> If your software DOES use a variable or a call from math.h (pow in this
> case),
> then linking with the math library is a must.
>
> Hope this answers your question, if it doesn't, post a test2.c with what =
you
> meant and I'll answer best I can.
>
> Nicolas.
>
>
>
> --
> FreeBSD 7.0-CURRENT #1: Sat Apr  8 21:33:25 EDT 2006
> root@clk01a:/usr/obj/usr/src/sys/CLK01A
> PGP? : http://www.clkroot.net/security/nb_root.asc
>
>



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?6a56d69c0604111627x78760975g8a8112c0d90f9ea>