PDA

View Full Version : math.h invisible?



jofre
February 24th, 2006, 12:06 PM
Ok lads, this is my first C program. Should be straight forward but gcc is complaining that " undefined reference to `pow' ", and yes I have " #include <math.h> " at the start.

Any ideas why?

Thanks a million, Jofre


Full code:
#include <stdio.h>
#include <math.h>

/* Declaring constants */
const double plank = 6.6261e-34 ; /* Planck's constant [J*s] */
const double c = 2.9979e8 ; /* speed of light [m/s] */

/* Declaring variables */
double density, intensity, wavelenght ;

int main(){
density = ( intensity * pow(wavelenght,4) ) / ( plank * pow(c,2) ) ;

printf("the density is %f", density) ;
}

Full answer form gcc:
$ gcc -g -Wall -o photon_density photon_density.c
photon_density.c: In function ‘main’:
photon_density.c:15: warning: control reaches end of non-void function
/tmp/ccyo0tCe.o: In function `main':
/c_code/photon_density.c:12: undefined reference to `pow'
collect2: ld returned 1 exit status

cstudent
February 24th, 2006, 12:25 PM
You need to add the -lm (that's LM) switch when you compile to link the math.h library.

btw, you have a typo in the word wavelength. :)

abhaysahai
February 24th, 2006, 12:27 PM
when in doubt use man
"man pow"
Link with -lm.

gcc -g -Wall -o photon_density photon_density.c -lm

Enjoy.

Abhay

jofre
February 24th, 2006, 12:38 PM
Thanks a million!!!

by the way, why in hell I did not found that in any of the C books I have in the office!!! (well just two, but still...)

Thanks again, Jofre.

Lux Perpetua
February 26th, 2006, 07:52 AM
It's actually a compiler-specific issue. Some compilers don't need to be told to link the math library, but gcc does.

linjava
December 25th, 2007, 06:37 PM
I know this is an old topic, but for those that have eclipse go to the particular project you're working on's properties by right clicking the project name in the project explorer to your left. Under GCC Linker go to the library section and simply add m (it will add the -l part so your m will turn into -lm). You should see in the build output the '-lm' at the end and you're good to go. BTW, I'm running kubuntu gutsy and using the eclipse cdt plugin with gcc.

virusmagic
October 10th, 2008, 08:38 PM
Excuse me. I am new in both Ubuntu and Eclipse. Could you explain it a little more please?

There is no choice like GCC Linker. If you could give a screenshot I would be grateful.

THankz.

LaRoza
October 10th, 2008, 10:09 PM
Excuse me. I am new in both Ubuntu and Eclipse. Could you explain it a little more please?

There is no choice like GCC Linker. If you could give a screenshot I would be grateful.

THankz.
Please start your own thread.

There are no screenshots needed for terminal commands, a copy and paste works just as well ;)