0

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Vivek S
  • 1
  • 2
  • Despite the duplicate being for C++ rather than C, many of the answers apply to the compilation systems for both. Specifically, function declaration in a header tells the compiler that "somewhere, a function of this name is available for use and this is its signature". The issue when creating a program is that you need to link the code that uses the function with the code that defines the function — the error message tells you that you didn't do that. You need to specify the object file or library (or source file) that defines the function in the command line that tries to link the program. – Jonathan Leffler Dec 16 '17 at 20:32

1 Answers1

1

The function needs to exist in the final executable, so you should compile both files together:

gcc one.c two.c -o two
    ~~~~~

Or more explicitly, link them together:

gcc -c one.c -o one.o
gcc -c two.c -o two.o
gcc one.o two.o -o two
#rm one.o two.o

As a side note, you should put variable declaration in a header file, and put its definition in one .c file, so it can be correctly linked.

iBug
  • 35,554
  • 7
  • 89
  • 134