Jump to content
  • Advertisement
Sign in to follow this  
nlbs

math.h pow() not taking variables in arguments

This topic is 3748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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]

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
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.

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
However I forgot how to add -lm option on KDevelop anybody help plese.

Edit:

Thanks I've fixed I need -lm Option.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!