From owner-freebsd-questions@FreeBSD.ORG Tue Apr 11 23:21:58 2006 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id B9D0E16A400 for ; Tue, 11 Apr 2006 23:21:58 +0000 (UTC) (envelope-from erikt@owl.midgard.homeip.net) Received: from pne-smtpout2-sn2.hy.skanova.net (pne-smtpout2-sn2.hy.skanova.net [81.228.8.164]) by mx1.FreeBSD.org (Postfix) with ESMTP id BA5E843D55 for ; Tue, 11 Apr 2006 23:21:57 +0000 (GMT) (envelope-from erikt@owl.midgard.homeip.net) Received: from falcon.midgard.homeip.net (83.253.29.241) by pne-smtpout2-sn2.hy.skanova.net (7.2.070) id 4430FC020022CE40 for freebsd-questions@freebsd.org; Wed, 12 Apr 2006 01:21:56 +0200 Received: (qmail 66208 invoked from network); 12 Apr 2006 01:21:56 +0200 Received: from owl.midgard.homeip.net (10.1.5.7) by falcon.midgard.homeip.net with SMTP; 12 Apr 2006 01:21:56 +0200 Received: (qmail 64878 invoked by uid 1001); 12 Apr 2006 01:21:56 +0200 Date: Wed, 12 Apr 2006 01:21:56 +0200 From: Erik Trulsson To: Jonathan Herriott Message-ID: <20060411232156.GA64832@owl.midgard.homeip.net> Mail-Followup-To: Jonathan Herriott , Nicolas Blais , freebsd-questions@freebsd.org References: <6a56d69c0604111554o587ce2c5ha1ff4ea20bbab0a4@mail.gmail.com> <200604111857.43171.nb_root@videotron.ca> <6a56d69c0604111607l5fba5939pfc6461a99a2ceab3@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6a56d69c0604111607l5fba5939pfc6461a99a2ceab3@mail.gmail.com> User-Agent: Mutt/1.5.11 Cc: freebsd-questions@freebsd.org, Nicolas Blais Subject: Re: pow function working unexpectedly X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 11 Apr 2006 23:21:58 -0000 On Tue, Apr 11, 2006 at 11:07:35PM +0000, 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. Because the compiler recognizes that pow() when called with only constants as arguments can be calculated at compile-time and does so. If you look at the generated assembler you will see that there is no call to pow() actually being made there -- it has been replaced by the appropriate constant. > > > On 4/11/06, Nicolas Blais 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 bug. > > > The problem seems to be with gcc when using the pow function in the > > > math.h library. Here's code that works: > > > > > > #include > > > #include > > > > > > int main() > > > { > > > printf("%f\n", pow(2,3)); > > > return 0; > > > } > > > > > > > > > Now, the following will not compile: > > > > > > #include > > > #include > > > > > > int main() > > > { > > > int x = 2; // I tried these as doubles too since the pow function > > > takes doubles > > > int y = 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 gcc > > > (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. > > -- Erik Trulsson ertr1013@student.uu.se