From: Ulrich Eckhardt on
zagreb wrote:
> define "modern GCC"

GCC is the GNU Compiler Collection. With modern, I mean anything since
version 4 (though version 3 is also not completely out-of-date). I tested
my code with version 4.2 for Linux/x86.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
From: zagreb on
On 17 ruj, 15:19, Ulrich Eckhardt <eckha...(a)satorlaser.com> wrote:
> GCC is the GNU Compiler Collection. With modern, I mean anything since
> version 4 (though version 3 is also not completely out-of-date). I tested
> my code with version 4.2 for Linux/x86.

Ok here's code:

user(a)linux:~/tmp$ uname -a
Linux linux 2.6.28-15-generic #49-Ubuntu SMP Tue Aug 18 18:40:08 UTC
2009 i686 GNU/Linux
user(a)linux:~/tmp$ gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
4.3.3-5ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.3/
README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/
usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-
included-gettext --enable-threads=posix --enable-nls --with-gxx-
include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-
clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --
enable-targets=all --with-tune=generic --enable-checking=release --
build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4)
user(a)linux:~/tmp$ cat pow1.c
#include <stdio.h>
#include <math.h>

int
main(void)
{

printf("%f\n", pow(2,3));
return 0;
}

user(a)linux:~/tmp$ gcc -o pow1 pow1.c
user(a)linux:~/tmp$ ./pow1
8.000000
user(a)linux:~/tmp$ cat pow3.c
#include <stdio.h>
#include <math.h>

int
main(void)
{
int x;
int y = 3;

for (x=2; x<11; x++)
printf("%f\n", pow(x,y));
return 0;
}

user(a)linux:~/tmp$ gcc -o pow3 pow3.c
/tmp/cciCYuL1.o: In function `main':
pow3.c:(.text+0x31): undefined reference to `pow'
collect2: ld returned 1 exit status
user(a)linux:~/tmp$ gcc -o pow3 pow3.c -lm
user(a)linux:~/tmp$ ./pow3
8.000000
27.000000
64.000000
125.000000
216.000000
343.000000
512.000000
729.000000
1000.000000
user(a)linux:~/tmp$


It is rather "modern GCC" since it is 4.3.3
Anyhow, it does not compile pow3.c program without math library linked
explicitly.
Program pow1.c is succesfully compiled because in fact it _does_not_
call pow at all.
Regards

From: Ulrich Eckhardt on
zagreb wrote:
> printf("%f\n", pow(2,3));
[..works..]

> for (x=2; x<11; x++)
> printf("%f\n", pow(x,y));
[..fails without -lm..]

> Program pow1.c is succesfully compiled because in fact it _does_not_
> call pow at all.

Interesting. I guess the compiler knows about pow and computes the value at
compile time, making any math libraries unnecessary, but I'm speculating
there.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
From: zagreb on
> >         for (x=2; x<11; x++)
> >         printf("%f\n", pow(x,y));
>
> [..fails without -lm..]

Exactly - gcc does not link math lib "per se"

> > printf("%f\n", pow(2,3));
>
> [..works..]
>
> Interesting. I guess the compiler knows about pow and computes the value at
> compile time, making any math libraries unnecessary, but I'm speculating
> there.
>

Try to compile it in assembly code - you'll see that it just prints
value:
.long 1090519040
(which is in fact floating point representation of number 8)
Regards


First  |  Prev  | 
Pages: 1 2
Prev: Basic, basic, C question!
Next: Bad pointer on CString