Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to pow( ) in C, despite including math.h [duplicate]

Tags:

c

Possible Duplicate:
Problem using pow() in C
what is 'undefined reference to `pow''

I'm having a bit of an issue with a simple piece of coursework for uni that's really puzzling me.

Essentially, I've to write a program that, amongst other things, calculates the volume of a sphere from a given radius. I thought I'd use the pow() function rather than simply using r*r*r, for extra Brownie points, but the compiler keeps giving me the following error:

undefined reference to 'pow' collect2: error: ld returned 1 exit status

My code looks like the following:

#include <math.h>

#define PI 3.14159265 //defines the value of PI

/* Declare the functions */
double volumeFromRadius(double radius);

/* Calculate the volume of a sphere from a given radius */
double volumeFromRadius(double radius) {
    return (4.0/3.0) * PI * pow(radius,3.0f);
}

and I'm compiling with the command gcc -o sphere sphere.c

This compiles and runs fine in code::blocks on the Windows machines at uni, but on my Fedora 17 at home the command line compiler refuses to run. Any thoughts would be gratefully appreciated!

Blessings, Ian

like image 879
Ian Knight Avatar asked Oct 10 '12 16:10

Ian Knight


People also ask

How do you fix undefined reference to POW in C?

a . You need to link your program with this library so that the calls to functions like pow() are resolved. This solved my issue.

Why is pow function not working in C?

On some online compilers, the following error may occur. The above error occurs because we have added “math. h” header file, but haven't linked the program to the following math library. Link the program with the above library, so that the call to function pow() is resolved.

What does undefined reference to POW mean in C?

When we use the pow function or any other math functions from math header in c program on linux environment (RHEL, Fedora or CentOS), need to use -lm option for compilation. otherwise you will get undefined reference to pow error, when you compile below program with no -lm option.

How do you use math h in GCC?

If you are compiling on the command-line with the gcc or g++ command, you would accomplish this by putting -lm at the end of the command. If you are going to compile a C program with math. h library in LINUX using GCC or G++ you will have to use –lm option after the compile command.


1 Answers

You need to link with the math library:

gcc -o sphere sphere.c -lm

The error you are seeing: error: ld returned 1 exit status is from the linker ld (part of gcc that combines the object files) because it is unable to find where the function pow is defined.

Including math.h brings in the declaration of the various functions and not their definition. The def is present in the math library libm.a. You need to link your program with this library so that the calls to functions like pow() are resolved.

like image 198
codaddict Avatar answered Sep 27 '22 23:09

codaddict