math.h pow() not taking variables in arguments

Started by
7 comments, last by Kambiz 15 years, 8 months ago
This is a C Problem. The Code Bellow Doesn't work it Fires Compile Time Error. Undefined reference to `pow'
#include <stdio.h>
#include <math.h>

int main(void){
  int i=1;
  printf("before for %d\n", pow(4, i));//Error Here
  return 0;
}



However the bellow one does compile.
#include <stdio.h>
#include <math.h>

int main(void){
  int i=1;
  printf("before for %d\n", pow(4, 2));//Error Here
  return 0;
}



Advertisement
Compiles fine on VC++ 2008 Express (and yes, I did remember to give the file a .c extension). What compiler are you using?

BTW, the return type of pow() is a double, so using %d to print it is incorrect. Use %lf %f or %g instead.

[Edited by - Gage64 on August 16, 2008 6:10:37 AM]
This is a peculiarity of your compiler. You should link in the Math library. Some compilers do this automatically and some do not. I don't know why the second piece of code should work if the first one doesn't.

Also remember that pow() returns a double, which mean that you cannot format it with "%d". Use "%f" or "%g" instead, or cast the result to int before you give it to printf.
Quote:Original post by Gage64
Use %lf instead.

%lf is for long double, %f is for double.
Quote:Original post by DevFred
Quote:Original post by Gage64
Use %lf instead.

%lf is for long double, %f is for double.


Whoops. sorry about that.
If you are using gcc you need to link to libm.so or libm.a:
gcc -lm -o test test.c

Since pow(4, 2) is a compile time constant, the compiler might replace it with 16.0 eliminating the pow and the need of linking to the math library.
Quote:Original post by Kambiz
If you are using gcc you need to link to libm.so or libm.a:
gcc -lm -o test test.c

Since pow(4, 2) is a compile time constant, the compiler might replace it with 16.0 eliminating the pow and the need of linking to the math library.

I am using gcc. I'll try your solution.
However I dont think that there is a typying problem. I've used different Variants too. However It was a quick post.
However I forgot how to add -lm option on KDevelop anybody help plese.

Edit:

Thanks I've fixed I need -lm Option.
From the KDevelop 3.3.x F.A.Q:

How to link against a .so library?


When your project is GNU Autotools (Autoconf/Automake) based then you are most probably using the KDevelop Automake manager (see FAQ#Where is the Automake manager in KDevelop?).
To link your program against a library, or to add a library do :


  • select the subproject you want to add the library to on the top view of the AutoMake manager.
  • select Options... with the RMB or hit the wrench icon
  • on the libraries ("compiler") tab add libs in the second box (link libraries outside of project). Just enter the lib name, without the lib prefix and the .a or .so or .la or what ever ending.
    • i.e if your lib is libsomelib.so, then you add it so: -lsomelib

  • If that library is not part of your system wide library path add <code>-Lpath/to/the/library/you/used</code> to the <code>LDFLAGS</code> in your Project->Project Options...->Configure Options->Linker Flags(LDFLAGS):.

Do not forget to add the the location of the library's .h files to the include path of your executable like it's explained in FAQ#How to add an external include path to a project?

This topic is closed to new replies.

Advertisement