Undefined reference error for a function [duplicate] - c

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.

Related

How to resolve "multiple definition of 'main()'" C compiling error?

I recently started learning C and must create program that scanf two integer values from standard input separated by a space then printf the sum of these two integers. Must be able to accept negative values. I'm using repl.it to write code 1st then pasting in .c to compile.
Attempt:
#include <stdio.h>
int main() {
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d", &number1);
scanf("%d", &number2);
// calculating sum
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
return 0;
}
[OP originally said "Except this prints" -- but this is not program output -- this is error output during the compilation process before the program ever ran]
Except when I try to compile the IDE outputs the errors
/tmp/t2-8eec00.o: In function `main':
t2.c:(.text+0x0): multiple definition of `main'
/tmp/t1-f81f83.o:t1.c:(.text+0x0): first defined here
/tmp/t3-72a7ab.o: In function `main':
t3.c:(.text+0x0): multiple definition of `main'
/tmp/t1-f81f83.o:t1.c:(.text+0x0): first defined here
/tmp/main-2c962b.o: In function `main':
main.c:(.text+0x0): multiple definition of `main'
/tmp/t1-f81f83.o:t1.c:(.text+0x0): first defined here
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
exit status 1
The output is wrong so what mistake did I make? What's the correct method to get expected values?
(eg 1+2=3)
platform using it on:
https://imgur.com/a/9E8RzAO
This is a project management issue. The IDE shows that you have 4 files, all of which conflict with each other. You have t1.c, t2.c, t3.c, and main.c. They all try to define main(), so in fact you have a 4-way conflict.
Functions in C exist in a global namespace across the whole project.
Remove all files from the project that don't have the version of main() you actually want, and re-compile. -- OR rename the functions in the other files to mutually distinct names different than main(). You may get a warning that these functions are never used, but the project will compile.
The error is saying that you have defined the main function multiple times, try to compile it in a new file. The code is correct, so try to compile the code by creating new file.

Square Root Program in C not Compiling [duplicate]

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

C round function is throwing error: "undefined reference to `round'..." [duplicate]

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.

library function error in C [duplicate]

This question already has answers here:
Undefined reference to `pow' and `floor'
(6 answers)
Closed 9 years ago.
I wrote this function
int *constructST(int arr[], int n)
{
// Allocate memory for segment tree
int x = (int)(ceil(log2(n))); //Height of segment tree
int max_size = 2*(int)pow(2, x) - 1; //Maximum size of segment tree
int *st = malloc(max_size*sizeof(int));
// Fill the allocated memory st
constructSTUtil(arr, 0, n-1, st, 0);
// Return the constructed segment tree
return st;
}
and I have included the following libraries math.h ,stdlib.h, stdio.h but I get the following error
/tmp/ccg4X72c.o: In function `constructST':
tree.c:(.text+0x3f4): undefined reference to `log2'
tree.c:(.text+0x40b): undefined reference to `ceil'
tree.c:(.text+0x433): undefined reference to `pow'
collect2: error: ld returned 1 exit status
Any help why I am getting this error though I have included the math.h .
Including <math.h> ensures that your program knows the function prototypes for these functions, so it can tell that you're calling them correctly, but it doesn't actually link the library code in to them. For that you will have to add the right linker flag when you build it, usually -lm:
gcc -o myprog myprog.c -lm

Using C math.h to calculate the sin of a number

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).

Resources