I am a beginner to C programming and I'm trying out different methods and experimenting with small programs.
I have four files. two header files and two source files. I want to:
- Declare a variable (Actually two variables and a char) and a function (from the second source file) in one header file.
- Define these variables and assign values to them in a second header file(variables from point 1).
- Write a function including these two header files and using the values from these files(point 2) in one source file (without a main- just a function definition using the variables).
- Have a main source file that invokes the function from the second source file (from point 3.)
How do I come about this? I have included both header files in both the .c files. But when I try to compile and link it (using GCC in Linux) I get a
multiple definition ... first defined here
error for all the variables.
I have looked at these answers First and Second I didn't quite understand the answers in the Second as I'm not able to figure out how to use header guards. I am not able to figure out how to check all of the boxes (points one through 4).
header1.h
extern int i,j; extern char c;
void ad_d();
header2.h
int j=6;int i=7;
char c='x';
fnfile.c
#include "header1.h"
#include "header2.h"
#include<stdio.h>
void ad_d()
{
i+=j;
printf("\n %d \t %c \n", i,c);
}
filemain.c
#include<stdio.h>
#include "header1.h"
#include "header2.h"
void main()
{
ad_d();
}