I am learning to share the 'functions' among a couple of files in C programming. So in order to share/access a user defined function which is created in one file (say one.c
), being in from another file (two.c
), we need to create a separate header file named after the file One, as one.h
and should declare the specified function in one.h
. Done. And then I have included the header file (one.h
) in both the files one.c
and two.c
by #include "one.h"
. Now when I call the function (of the file one.c
), that will find the area of the circle from two.c
. I am getting a compilation error as
C:\Users\Lenovo\AppData\Local\Temp\cceCxXJH.o:two.c:(.text+0x36): undefined reference to `area_circle'
Name of my funtion is area_circle
. Please help me whether the way I did is right to share functions among files.
File one.c
:
#include<stdio.h>
#include "One.h"
float pi = 3.14;
float radius;
void main()
{
printf("Enter the radius: ");
scanf("%f", &radius);
float c_area = area_circle(radius);
float c_circum = circum_circle(radius);
}
float area_circle(float r)
{
float area = pi * r * r;
printf("Area of the circle: %.2f", area);
return area;
}
float circum_circle(float rad)
{
float circum = 2 * pi * rad;
printf("\nCircumference of the circle: %.2f", circum);
return circum;
}
File one.h
float area_circle(float r);
float circum_circle(float rad);
float pi;
File two.c
#include <stdio.h>
#include "one.h" //Included the header file in which the functions are declared.
void main()
{
extern float pi; //Global variable in One.c
float rds;
printf("Enter the radius: ");
scanf("%f", &rds);
float are = area_circle(rds); //Function built in One.c; Declared in One.h
float cir = circum_circle(rds); //Function built in One.c; Declared in One.h
}
I used gcc two.c -o two && two
to compile and run the program. Kindly help me with this.