[Solved] Undefined Reference To pow Function

What is a pow function?

Pow() is a predefined function in math.h header file of C++ programming. It is a power function that is used to calculate the power of a given number. To use this function you must include math.h header file in your program.

Syntax of a pow() function:

double pow(double base_value, double power_value)

base_value: Input number whose power to be calculated

power_value: required power of an input number

Example: Calculate the square of a 5

double result = pow(5, 2);

Why you are getting undefined reference to pow function error message?

undefined reference to pow

If you are getting this error message means you are using the pow() function in your program to calculate the required power of a given number. But the compiler is not able to find the required header file information where the pow() function is defined. There are mainly following two reasons which may cause this error message:

A. Missing header file (math.h )from your program

B. Missing required argument (-lm) while compiling your program

It’s a very common error when you tried to compile your program on a Linux environment as a regular program. Don’t worry and keep reading this article to get rid of this error message.

How to solve undefined reference to pow function error?

To fix the above error message make sure you have followed the following two points.

1. Include the required header file (math.h) in your program as shown below

#include<math.h>

2. Add libm.a library file path in the arguments while compiling your program

When we add the math.h header file in our program then it adds the declaration of pow function in the program not the definition of the function. A detailed function definition is available in the libm.a library file. The library is nothing but the collection of different object files.

On linux environment libm.a files is stored in the ‘/usr/lib/libm.a’ location.

We have to pass the path of this library file location in an argument while compiling the program on a terminal. There are two ways you can give the path of this library in the arguments

Give complete path of library file as shown below:

gcc –o SampleProgram SampleProgram.c /usr/lib/libm.a

You can also directly specify -lm in the arguments. It will automatically pick math library function file as shown below:

gcc –o SampleProgram SampleProgram.c -lm
Note: Its Recommended to use second approach (-lm) of providing linkage of library files in the arguments. 

Final Thoughts:

We hope this article was informative and you are able to get rid of the error message “undefined reference to pow”. If you are still facing the issue then please do mention it in the comment section or you can reach out to us using the contact form.

2 thoughts on “[Solved] Undefined Reference To pow Function”

  1. I see this explanation everywhere. How does it explain this?

    #include
    #include
    int main() {
    // This works fine…
    double B = pow(2.00,4.00);
    printf(“Test the pow function: %lf”, B);

    // but this does NOT work!
    double radius;
    scanf(“%lf”, &radius);
    area = pow(radius, 2); // Generates the error!
    }

    replacing the radius variable with a numeric literal, like 2, makes it work. Why doesn’t pow() accept a defined variable?

    Reply

Leave a Comment