First, I have created two header files: add.h and subtract.h
Next, created two source files: add.c and subtract.c.
Finally, created main program in main.c:
gcc compiler
Code:
//add.h
#ifndef ADD_H
#define ADD_H
int add(int x, int y);
#endif
//subtract.h
#ifndef SUBTRACT_H
#define SUBTRACT_H
int subtract(int x, int y);
#endif
//add.c
#include "add.h"
int add(int x, int y) {
return x + y;
}
//subtract.c
#include "subtract.h"
int subtract(int x, int y) {
return x - y;
}
//main.c
#include <stdio.h>
#include "add.h"
#include "subtract.h"
int main() {
int x = 10, y = 5;
printf("x + y = %d\n", add(x, y));
printf("x - y = %d\n", subtract(x, y));
return 0;
}
Here's the compile command:
gcc -o main add.c subtract.c main.c
Code:
main.c: In function 'main':
main.c:12:28: warning: implicit declaration of function 'subtract' [-Wimplicit-function-declaration]
printf("x - y = %d\n", subtract(x, y));
^~~~~~~~
C:\Users\33\AppData\Local\Temp\ccfJeU2u.o:subtract.c:(.text+0x0): multiple definition of `add'
C:\Users\33\AppData\Local\Temp\ccGHzJAD.o:add.c:(.text+0x0): first defined here
C:\Users\33\AppData\Local\Temp\ccqKrrPu.o:main.c:(.text+0x0): multiple definition of `add'
C:\Users\33\AppData\Local\Temp\ccGHzJAD.o:add.c:(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status