Results 1 to 7 of 7
Hey..im writing a simple application using the pow() function on linux..
the problem is that considering the following code:
int x = 2;
double z = pow(x,2);
this works perfectly ...
- 10-12-2007 #1Just Joined!
- Join Date
- Oct 2007
- Posts
- 60
pow() function problem
Hey..im writing a simple application using the pow() function on linux..
the problem is that considering the following code:
int x = 2;
double z = pow(x,2);
this works perfectly fine..compiles and runs normally on both windows and linux..
considering this code however:
int x = 2;
double z = pow(2,x);
this creates an error: "/tmp/ccy3HUjD.o: In function `main':
powers_one.c
.text+0x309): undefined reference to `pow'
collect2: ld returned 1 exit status"
BUT IT WORKS ON WINDOWS!...any idea whats the problem?
- 10-12-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Did you use the -lm compile flag?
Regards
- 10-12-2007 #3Just Joined!
- Join Date
- Oct 2007
- Posts
- 60
- 10-12-2007 #4Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
The standard C library math functions are not included in libc so you have to compile your source with the -lm flag as follow:
RegardsCode:gcc powers_one.c -o powers_one -lm
- 10-12-2007 #5Just Joined!
- Join Date
- Oct 2007
- Posts
- 60
- 10-12-2007 #6Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
There are several mathematical functions (e.g. sin, cos, sqrt, exp, log, etc.) provided in the maths library /usr/lib/libm.a.
The math library is separate from the regular C library so you need to explicitly link in the math library, using the -lm flag.
Regards
- 10-13-2007 #7
More specifically, "-lSTRING" finds the library that is named STRING and uses the definitions described in it. You need to do this whenever you use a library that is not libc (the standard C library).
So for instance, I was doing some work with Ogg Vorbis files, and I was using some functions defined in libvorbis. Therefore, when I compiled, I had to add "-lvorbis" to the gcc call.
Franklin's explanation explains why you need this for the math functions.DISTRO=Arch
Registered Linux User #388732


Reply With Quote
