Sign in to follow this  
nlbs

math.h pow() not taking variables in arguments

Recommended Posts

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;
}



Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this