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
Related
#include <stdio.h>
#include <math.h>
int exp_for_level(int n) {
return (int)(100 * pow(n, 2.3));
}
int main(){
int x;
x = exp_for_level(6);
printf("%d", x);
return 0;
}
I receive the following error when I run this code on an online compiler
/tmp/cc28S7ML.o: In function exp_for_level':
main.c:(.text+0x19): undefined reference to `pow'
collect2: error: ld returned 1 exit status
How do I rectify this?
After I couldn't get it to work on the online compiler, I followed advice from some other threads on
The file is stored under a file grades.c on my mac
I've tried entering this
$ gcc - Wall - lm -o grades grade . c
into my terminal and i just get zsh error: command not found
Any ideas on what the issue is here too?
The online compiler I'm using is
https://www.tutorialspoint.com/compile_c_online.php
EDIT: in my post, in main I'd miswritten the function as exp_to_level instead of exp_for_level. Didn't copy paste the entire code as it's too long. I narrowed it down and retyped it to the portion that yields the error.
There are some errors in your code, you have defined a function exp_for_level but you use exp_to_level.
Then your x variable is not defined
If you fix your code like this:
#include <stdio.h>
#include <math.h>
int exp_for_level(int n) {
return (int)(100 * pow(n, 2.3));
}
int main(){
int x = exp_for_level(6);
printf("%d", x);
return 0;
}
and you compile:
gcc -Wall powtest.c -o powtest -lm
it works.
About the error on the online compiler:
The undefined reference error occurs because you are missing -lm linker option.
Edit the online compiler command clicking on Project->Compile Options:
About this problem on your local machine:
After I couldn't get it to work on the online compiler, I followed
advice from some other threads on The file is stored under a file
grades.c on my mac I've tried entering this
$ gcc - Wall - lm -o grades grade . c
into my terminal and i just get zsh error: command not found
you don't have the compiler installed.
You should install clang, Have a look to this question
First of all your function name is wrong in the main take a look here exp_for_level
and in main its exp_to_level change one of them then also add int x in main to solve the issue.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I am building a program that calculates the surface area of a sphere. I also made an additional feature that allows the user to input a positive number only. Before setting my loop, I defined the function for the surface area, but it wouldn't compile because the function is not defined. The code is displayed:
include <stdio.h>
#define PI 3.1415
float sa_sphere(float r);
int main()
{
float r;
do{
printf("Enter a positive radius: ");
scanf("%f", &r);
if (r<0){
printf("Error: number entered is not positive.\n");}
}
while (r<0);{
printf("The surface area is: %.2f\n",sa_sphere(r));}
return 0;
}
I am using Linux Mint to compile it — gcc gg.c then ./a.outwhere gg is my file name.
/tmp/ccRUfp76.o: In function `main':
gg.c:(.text+0x6e): undefined reference to `sa_sphere'
collect2: error: ld returned 1 exit status
I would appreciate any tip to solve it. Please don't display the code in the answer, though.
You did not define function float sa_sphere(float r); so during linking compiler raised linking error. You must define the function in your code. If your are new please check what is function definition means. GOOD LUCK.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
I hope someone can help me. I am working through CS50x and am working on Pset1 - greedy. I am getting the following error whenever I compile my code:
/tmp/greedy-46be96.o: In function `main':
greedy.c:(.text+0x95): undefined reference to `round'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any help will be greatly appreciated. I apologise if the question is vague, I have tried to be in depth. I have used man round in the terminal, and have searched everywhere, trying different solutions, but nothing has worked.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
float owed;
float change;
float quarter = 0.25;
float dime = 0.10;
float nickel = 0.05;
float penny = 0.01;
do {
printf("How much change is owed?: ");
owed = GetFloat();
} while(owed <= 0);
change = round(owed * 100);
}
I am using this command to compile my code:
clang -o greedy greedy.c -lcs50
The following should work when you compile:
clang -o greedy greedy.c -lcs50 -lm
This links the math library for the compiler.
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).