How to solve "Undefined reference to 'sqrt' Error

posted 6 min read

Introduction :

If you are a newbie to C or C++ and are working with a math library, then you might get this error  Undefined reference to `sqrt' . This error indicates that the compiler or linker couldn't find the definition for the sqrt function, which is used to calculate the square root of a number. To resolve this, you need to link your program with the math library. In your terminal, if you're using the GCC compiler for example, you can do this by adding -lm at the end of your command line. This ensures that the math library is correctly linked and the sqrt function is recognized. In this blog post, I will guide you in detail on how can you resolve this error.

Tip: Always ensure that you link your program to the math library by adding -lm at the end of your command line when you compile your program. E.g. when you compile your C program run a command like this in the terminal. here Main.c is the program that you want to compile and main is the program that you will get after compilation. gcc -o main Main.c -lm

Why did this error happen?

This error generally occurs when the math library, which contains the definition of the sqrt function, is not correctly linked to your program. (This error message indicates that when compiling or linking your code, the system couldn't find the necessary instructions for the sqrt function, which is commonly used for calculating square roots. This usually happens when the program is unable to locate the library or source code where sqrt is defined.) Without this link, your program doesn't know how to execute the sqrt function, hence the "undefined reference" error.

Here is a code block that might produce this error:

#include <stdio.h>

int main() {
   double num = 9.0;
   double root = sqrt(num);
   printf("square root of %.2f is %.2f", num, root);
   return 0;
}

Error by Compiler:

Causes for the "Undefined reference to `sqrt'" Error

1. Lack of Proper Linkage to the Math Library

The math library houses the 'sqrt' function definition. If your program is not properly linked to this library, it will not know how to execute the 'sqrt' function, hence the "undefined reference" error. For example, the following code snippet will produce an error because it does not link to the math library:

    #include<math.h>
    #include<stdio.h>
    
    int main() {
       double num = 9.0;
       double root = sqrt(num);
       printf("square root of %.2f is %.2f", num, root);
       return 0;
    }

To fix this, add -lm at the end of your command line when you compile your program. This ensures that your program is linked to the math library.

2. Syntax Error or Incorrect Usage of the 'sqrt' Function

Even if your program is properly linked to the math library, it can still produce an "undefined reference" error if there's a syntax error or incorrect usage of the 'sqrt' function. For example, forgetting to include the math library before using the 'sqrt' function will produce an error:

Error code:

#include <stdio.h>

int main() {
   double num = 9.0;
   double root = sqrt(num);
   printf("square root of %.2f is %.2f", num, root);
   return 0;
}
Caution: Don't miss to add " math library " at the header.

To fix this, ensure that you include the math library at the start of your program by adding #include <math.h>

Corrected code:

#include <stdio.h>
#include <math.h>

int main() {
   double num = 9.0;
   double root = sqrt(num);
   printf("square root of %.2f is %.2f", num, root);
   return 0;
}

3. Using an Outdated or Incompatible Compiler

Using an outdated or incompatible compiler might trigger this error. Different compilers might have different methods for linking libraries or different requirements for syntax. Make sure you're using a compiler that's compatible with your code and with the libraries you're using.

Solution for "Undefined reference to `sqrt'" Error

1. Properly Link Your Program to the Math Library

The most common cause of this error is not linking the math library correctly. You need to link your program to the math library in your code. This can be done by adding -lm at the end of your command when compiling your program. Here is how you can do it:

gcc -o program program.c -lm

In this command, program.c is the file you want to compile, and program is the name of the output file. The -lm flag ensures that the math library is linked properly.

2. Check Your Syntax and Usage of 'sqrt' Function

The 'sqrt' function is defined in the math library, so you need to ensure that you're including this library in your program. If you forget to do so, you'll get an "undefined reference" error. Here's how you can include the math library:

#include <math.h>

Here's an example of a program that correctly includes the math library and uses the 'sqrt' function:

#include <stdio.h>
#include <math.h>

int main() {
   double num = 16.0;
   double root = sqrt(num);
   printf("square root of %.2f is %.2f", num, root);
   return 0;
}

3. Use a Compatible Compiler

Different compilers might have different ways of linking libraries and different requirements for syntax. If you're still encountering the "undefined reference" error, you might want to try using a different compiler. For example, if you're currently using gcc, you might want to try using clang instead. Here's how you can do that:

clang -o program program.c -lm

In this command, **program.c** is the file you want to compile, and **program** is the name of the output file. The -lm flag ensures that the math library is linked properly.

Remember, the key is to ensure that your program is correctly linked to the math library, and to check your usage of the 'sqrt' function. If you keep these points in mind, you should be able to avoid the "undefined reference to `sqrt'" error.

Frequently Asked Questions

1. What does the "Undefined reference to `sqrt'" error mean? Ans. This error means that the compiler or linker couldn't find the definition for the sqrt function, which is used to calculate the square root of a number.
2. Why does the "Undefined reference to `sqrt'" error occur? Ans. This error generally occurs when the math library, which contains the definition of the sqrt function, is not correctly linked to your program. Without this link, your program doesn't know how to execute the sqrt function, hence the "undefined reference" error.
3. How do I fix the "Undefined reference to `sqrt'" error? Ans. You just need to link your program to the math library in your code. You can do this by adding lm at the end of your command line when you compile your program.
4. Why do I still get the "Undefined reference to `sqrt'" error even after linking to the math library? Ans. Even if your program is properly linked to the math library, it can still produce an "undefined reference" error if there's a syntax error or incorrect usage of the 'sqrt' function. Make sure to include the math library at the start of your program and check your usage of the 'sqrt' function.
5. Can using an outdated or incompatible compiler cause the "Undefined reference to `sqrt'" error? Ans. Yes, using an outdated or incompatible compiler might trigger this error. Different compilers might have different methods for linking libraries or different requirements for syntax. Make sure you're using a compiler that's compatible with your code and with the libraries you're using

Conclusion :

In conclusion, the "Undefined reference to 'sqrt'" error is a common issue faced by beginners in C or C++ programming. Understanding the causes and knowing how to resolve this error is crucial. Properly linking your program to the math library, checking your syntax and usage of the 'sqrt' function, and using a compatible compiler are the key steps to solving this error. Always remember to include the math library at the start of your program and check your usage of the 'sqrt' function. By following these steps, you should be able to compile and run your programs successfully without encountering this error. Happy coding!

1 Comment

0 votes

More Posts

How to Generate Random Numbers from 1 to 6 in C++

Danny Jay - Nov 23

How to Write a program to find the sum of the following series 1^1+2^2+3^3... in Python,Java and C

Peter Jones - Nov 23

Hello World! Welcome to the C/C++ Group!

James Dayal - Sep 25

Transitioning from Assembly to C: Challenges and Solutions for Programmers Effectively

Aditya Pratap Bhuyan - Jun 22

How to solve macos hdmi sound control problem

Ozkan Pakdil - Nov 21
chevron_left