Error - undefined reference to 'sqrt' in Linux.

Fixing undefined reference to 'sqrt' in Linux

This is a common error while compiling C program in GCC/G++ Linux. This error occurs when you are using sqrt function to find square root of a number in your programs.

To fix this problem ensure following points

  • Include header file math.h in your program.
  • Add –lm linker flag with compilation command.

1- Include Header file


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

2- Compile command

 gcc  main.c -o main -lm

To more explanation : Compiling C program with math.h library in Linux.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.