0

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Exactly that. The function is not defined, there is no implementation of the function available. – Weather Vane Feb 24 '18 at 21:54
  • 2
    You didn't define the function `sa_sphere()`; you _declared_ it, which tells the compiler that it exists somewhere. Unfortunately, you've not actually supplied the definition — and you need to write the code `float sa_sphere(float r) { …calculation…; return surface_area; }` or similar, except you'll provide the details of the calculation. That can be in the same source file as `main()` — or possibly in a separate source file, but then you should declare the function in a header file and include the header in both `gg.c` and wherever you define `sa_sphere()`, and your compilation is harder. – Jonathan Leffler Feb 24 '18 at 22:21

1 Answers1

0

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.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17