When compiling a C program that uses the pow function, you might encounter the following error message:
undefined reference to `pow'Why Does undefined reference to `pow' error Occurs
The pow function is part of the math library in C, which is not linked by default when you compile your program. The math library (libm) contains mathematical functions like pow, sqrt, sin, and many others. If you do not explicitly link this library during the compilation, the linker will not be able to find the definition of pow, resulting in an "undefined reference" error.
For example: Let us look into the C program to find Armstrong numbers between given interval
#include <stdio.h>
#include <math.h>
// Function to count the number of digits in a number
int countDigits(int num) {
int count = 0;
while (num != 0) {
num /= 10;
++count;
}
return count;
}
// Function to check if a number is an Armstrong number
int isArmstrong(int num) {
int originalNum = num;
int n = countDigits(num);
int result = 0;
while (num != 0) {
int remainder = num % 10;
result += pow(remainder, n);
num /= 10;
}
return (result == originalNum);
}
int main() {
int low = 100, high = 500; // Define the range in the driver code
printf("Armstrong numbers between %d and %d are: ", low, high);
// Iterate through each number in the interval
for (int i = low + 1; i < high; ++i) {
if (isArmstrong(i)) {
printf("%d ", i);
}
}
return 0;
}
Expected output:
153, 370, 371, 407Actual Output (Error):
/tmp/ccGJHZ8f.o: In function `isArmstrong':
Solution.c:(.text+0xa6): undefined reference to `pow'
collect2: error: ld returned 1 exit status
Solution for undefined reference to `pow' in C language
To solve the "undefined reference to pow" error, follow these steps:
- Include the Math Header: Ensure your code includes the math header.
#include <math.h> - Link the Math Library: Use the
-lmflag when compiling your code. Here are some examples for different compilers:- GCC (GNU Compiler Collection):
gcc -o myprogram myprogram.c -lmThis tells GCC to link the math library with your program. - Clang:
clang -o myprogram myprogram.c -lm - Other Compilers: Most C compilers use a similar flag. Refer to your compiler's documentation if you are using a different one.
- GCC (GNU Compiler Collection):
Example
Here's a complete example demonstrating a simple program that uses the pow function, and how to compile it correctly.
Program (armstrong_in_interval.c):
#include <stdio.h>
#include <math.h>
// Function to count the number of digits in a number
int countDigits(int num) {
int count = 0;
while (num != 0) {
num /= 10;
++count;
}
return count;
}
// Function to check if a number is an Armstrong number
int isArmstrong(int num) {
int originalNum = num;
int n = countDigits(num);
int result = 0;
while (num != 0) {
int remainder = num % 10;
result += pow(remainder, n);
num /= 10;
}
return (result == originalNum);
}
int main() {
int low = 100, high = 500; // Define the range in the driver code
printf("Armstrong numbers between %d and %d are: ", low, high);
// Iterate through each number in the interval
for (int i = low + 1; i < high; ++i) {
if (isArmstrong(i)) {
printf("%d ", i);
}
}
return 0;
}
Compiling the Program:
gcc -o armstrong_in_interval armstrong_in_interval.c -lmRunning the Program:
./armstrong_in_intervalExpected Output:
153, 370, 371, 407What to Look Out For
The "undefined reference to pow" error occurs because the math library is not linked by default when you compile a C program. To resolve this, include the math header file in your code and link the math library explicitly using the -lm flag during compilation.
- Check Your Code: Ensure that you have included the correct header file at the beginning of your code:
#include <math.h> - Link the Math Library: When compiling your program, you need to link the math library explicitly. This is done using the
-lmflag with your compiler command.
By following these steps, you can successfully use the pow function and other math library functions in your C programs.