public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* sqrt(x)
@ 2005-05-15 15:50 tobaccofarm
  2005-05-15 16:11 ` sqrt(x) Wolfgang Formann
  2005-05-15 21:15 ` sqrt(x) Eljay Love-Jensen
  0 siblings, 2 replies; 3+ messages in thread
From: tobaccofarm @ 2005-05-15 15:50 UTC (permalink / raw)
  To: gcc-help

Hello,

I tried to compile the following simple c program:


-------------------------------------------------------------------------
#include <stdio.h>
#include <math.h>

main()
{
   double x, y, z;

   x = 64.0;
   y = 3.0;
   z = 0.5;
   printf("pow(64.0, 3.0) returns: %7.0f\n", pow(x, y));
   printf("sqrt(64.0) returns:         %2.0f\n", sqrt(x));
   printf("pow(64.0, 0.5) returns: %2.0f\n", pow(x, z));
   return 0;
}

------------------------------------------------------------------------

However the compiler gave the following error messages:

sqrt.c:15:2: warning: no newline at end of file
/tmp/ccCL96r1.o(.text+0x4d): In function `main':
sqrt.c: undefined reference to `pow'
/tmp/ccCL96r1.o(.text+0x80):sqrt.c: undefined reference to `sqrt'
/tmp/ccCL96r1.o(.text+0xc0):sqrt.c: undefined reference to `pow'
collect2: ld returned 1 exit status


gcc  -v gives:

Reading specs from /usr/lib64/gcc-lib/x86_64-suse-linux/3.3.5/specs
Configured with: ../configure --enable-threads=posix --prefix=/usr
--with-local-prefix=/usr/local --infodir=/usr/share/info
--mandir=/usr/share/man --enable-languages=c,c++,f77,objc,java,ada
--disable-checking --libdir=/usr/lib64 --enable-libgcj
--with-slibdir=/lib64 --with-system-zlib --enable-shared
--enable-__cxa_atexit x86_64-suse-linux
Thread model: posix
gcc version 3.3.5 20050117 (prerelease) (SUSE Linux)

I tried to compile the above c file on knoppix 2.8.2,the same error message
was displayed.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: sqrt(x)
  2005-05-15 15:50 sqrt(x) tobaccofarm
@ 2005-05-15 16:11 ` Wolfgang Formann
  2005-05-15 21:15 ` sqrt(x) Eljay Love-Jensen
  1 sibling, 0 replies; 3+ messages in thread
From: Wolfgang Formann @ 2005-05-15 16:11 UTC (permalink / raw)
  To: gcc-help

tobaccofarm wrote:
> However the compiler gave the following error messages:
> 
> sqrt.c:15:2: warning: no newline at end of file
> /tmp/ccCL96r1.o(.text+0x4d): In function `main':
> sqrt.c: undefined reference to `pow'
> /tmp/ccCL96r1.o(.text+0x80):sqrt.c: undefined reference to `sqrt'
> /tmp/ccCL96r1.o(.text+0xc0):sqrt.c: undefined reference to `pow'
> collect2: ld returned 1 exit status

No, it was the linker.
No, you are on the wrong list.
Yes, try out -lm, then it should work.

Wolfgang

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: sqrt(x)
  2005-05-15 15:50 sqrt(x) tobaccofarm
  2005-05-15 16:11 ` sqrt(x) Wolfgang Formann
@ 2005-05-15 21:15 ` Eljay Love-Jensen
  1 sibling, 0 replies; 3+ messages in thread
From: Eljay Love-Jensen @ 2005-05-15 21:15 UTC (permalink / raw)
  To: tobaccofarm, gcc-help

Hi tobaccofarm,

In addition to what Wolfgang mentioned, the %f specifies a float, not a double.  The %lf specifies a double (which you are using), and %Lf specifies a long double.

Since in C (as I recall, prehaps incorrectly), a float is promoted to a double before passing into the printf routine, I'm not sure if that means that %f and %lf have the same behavior.  Just a FYI, in case it is relevant.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-05-15 21:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-15 15:50 sqrt(x) tobaccofarm
2005-05-15 16:11 ` sqrt(x) Wolfgang Formann
2005-05-15 21:15 ` sqrt(x) Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).