This question already has answers here:
Undefined reference to sqrt (or other mathematical functions)
(5 answers)
Closed 5 years ago.
So I'm trying to make a program that calculates the quadratic formula, but when I try to compile the code, I get the following:"undefined reference to sqrt"
But I tried defining sqrt via math.h and 2 other times in the code.
I have attached my code
Any help would be greatly appreciated
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double sqrt(double);
int main (void) {
double sqrt(double);
int a,b,c;
double discriminant,squarerootofdis,root1, root2;
printf("Please enter the coefficient of x^2:");
scanf("%d",&a);
printf("Please enter the coefficient of x:");
scanf("%d",&b);
printf("Please enter the integer value of the ploynomial:");
scanf("%d",&c);
if (a==0 && b==0)
{printf("This case is extremely degenerate");}
else if (a==0 && b!=0)
{root1=-c/b;
printf("Degenerate one real root: %lf\n",root1);}
else{
discriminant = ((b*b)-(4*a*c));
squarerootofdis = sqrt(discriminant);
root1 = (squarerootofdis-b)/(2*a);
root2 = (-squarerootofdis-b)/(2*a);
if (discriminant>0)
printf("Two real roots: %lf\n %lf\n", root1, root2);
else if (discriminant == 0)
printf("Degenerate one real root: %lf\n",root1);
else if (discriminant<0)
printf("Two complex roots: %lf\n %lf\n", root1, root2);
}
}
To use the sqrt function (or any function defined in math.h), you'll have to link the m library:
~$ gcc -lm yourcode.c -o program
Did you compile with -lm linked?
Header file will provide the decalration to the sqrt() function. To have the definition, you need to link with the math library consisting of the function definition.
Example:
gcc test.c -o output -lm
Please use the below command
gcc test.c -lmath
Related
This question already has answers here:
M_PI not available with gcc --std=c11 but with --std=gnu11?
(3 answers)
Closed 1 year ago.
I have the following code:
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>
char r[15];
double radius;
double SphereVolume(double radius){
double volume;
volume = (4.0 / 3.0) * (M_PI) * (pow(radius, 3.0)); ///'identifier "M_PI" is undefined'
return (volume);
}
int main()
{
/* gets input from the user */
printf("What is the radius of the Sphere? ");
fgets(r, sizeof(r), stdin);
sscanf(r, "%lf", &radius);
printf("Volume of the sphere is %f", SphereVolume(radius));
return (0);
}
I can run it well with gcc by doing:
$> gcc -Wall -Werror -Wextra -o SphereVolume SphereVolume.c -lm
But VS Code isn't letting me even debugging the code. What is happening? do I have messed up settings?
I think I found the answer:
M_PI not available with gcc --std=c11 but with --std=gnu11?
Apparently M_PI is not defined in standard C
This question already has answers here:
Why do you have to link the math library in C?
(14 answers)
Closed 4 years ago.
Just starting to learn C, and I found this example program on a C tutorial website, and it is giving an error upon compiling.
Here is the program, calculates the square root of a number based on user input:
#include <stdio.h>
#include <math.h>
int main()
{
double num, root;
/* Input a number from user */
printf("Enter any number to find square root: ");
scanf("%lf", &num);
/* Calculate square root of num */
root = sqrt(num);
/* Print the resultant value */
printf("Square root of %.2lf = %.2lf", num, root);
return 0;
}
I compile it using gcc in Ubuntu:
gcc -o square_root square_root.c
And here is the error:
/tmp/cc9Z3NCn.o: In function `main':
square_root.c:(.text+0x4e): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
What am I doing wrong? I can see that the math module is imported, so why the error?
Again, I just started studying C today, and I just want to figure out how to get programs to run. Thank you for your patience as it must be something obvious.
sqrt lives in the math library, so you need to tell your program to link to it with -lm:
gcc -o square_root square_root.c -lm
You need to compile it with -lm flag
gcc -o square_root square_root.c -lm
This question already has answers here:
Why do you have to link the math library in C?
(14 answers)
Closed 9 years ago.
I also entered include<math.h> but it still doesnt work. People are saying to enter -Im but im new to this where do I put -Im and how do I fix this.
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
float a=0, b=0, c=0, root1=0, root2=0;
printf("Enter the value of a,b and c to determine the roots\n");
scanf("%f%f%f",&a,&b,&c);
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root1=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("The first roots of the quadratic equation are\nFirst root=%.1f\nSecond root=%.1f",root1,root2);
return 0;
}
You have a copy-paste bug here:
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root1=(-b-sqrt(b*b-4*a*c))/(2*a);
should be:
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root2=(-b-sqrt(b*b-4*a*c))/(2*a);
Also you may need to link with the math library, e.g.
$ gcc -Wall foo.c -o foo -lm
Two things: first you copy pasted "root1" twice so you will lose the "plus" value and root2 will be zero. Second, for the benefits of others, the problem is most probably at compile time and the googled answer is there:
http://www.cs.cf.ac.uk/Dave/C/node17.html
And you should test for imaginary values:
if(b*b-4*a*c < 0){
printf("error: complex solution unsupported, see http://en.wikipedia.org/wiki/Square_root\n");
exit(1);
}
I'm an absolute beginner to C and I've read a few books but never really played with it. I'm starting to try to apply what I've read with a very simple program that returns the sin of a number. The hardest thing I've encountered with C is knowing how and when to use pointers.
I'm sure this is simple but here is how I've written my test:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x;
printf("Enter a number to calculate the sin(x): \n");
scanf("%lf", &x);
printf("sin(%lf) = %lf\n", x, sin(x));
return 0;
}
I'm compiling and executing this code in Ubuntu
gcc -lm sinCalc.c && ./a.out
Error I'm receiving is this:
/tmp/blaha.o: In function `main':
sinCalc.c:(.text+0x31): undefined reference to `sin'
collect2: ld returned 1 exit status
Undefined symbols are resolved left to right, so
gcc sinCalc.c -lm && ./a.out
should work.
Are they [structs] like an interface in Java?
No. Structs are an aggregate of a number (1 or more) of types that can be dealt with as a single unit in certain circumstances (assignment, parameter passing).
/tmp/ccjiJKv2.o: In function func':
b.c:(.text+0x16b): undefined reference tosqrt'
collect2: ld returned 1 exit status
Here is the code and I am using gcc compiler..
/*Write a function that receive 5 integers and returns the sum,average and standard deviation of these numbers*/
/*Author:Udit Gupta Date:10/08/2011*/
#include<stdio.h>
#include<math.h>
void func (int *,float *,float *);
int main () {
float avg,std_dev;
int sum;
func (&sum,&avg,&std_dev); /*Passing address of variables where output will be stored.....*/
printf ("The sum of numbers is %d\n",sum);
printf ("The average of numbers is %f\n",avg);
printf ("The standard deviation of numbers is %f\n",std_dev);
}
void func (int *sum_, float *avg_ , float * std_dev_) {
int n1,n2,n3,n4,n5;
printf("Please enter the number:");
scanf("%d%d%d%d%d",&n1,&n2,&n3,&n4,&n5);
/*Formula for sum,average and standard deviation*/
*sum_ = n1+n2+n3+n4+n5; /*Writing output at the address specified by arguments of function*/
*avg_ = *sum_ / 5 ;
*std_dev_ = sqrt ( pow((n1-*avg_),2)+pow((n2-*avg_),2)+pow ((n3-*avg_),2)+pow ((n4-*avg_),2)+pow ((n5-*avg_),2)/4) ;
}
You are not linking the library which contains the implementation of sqrt.
That library is known as "libm" (the math library), and can be linked to as follows:
gcc -o myprog infile.c -lm
#include <math.h> - is this what you need?
Also, Link with -lm
you have to link the math library. -lm