I am attempting to create a makefile for a simple program which relies on a simple file and a function stored in a c file. Here are the files:
function.c:
int random_fun(int n, int m)
{ int g;
n = n+m;
m=n/3;
g=n*m;
return g;
}
main.c:
#include <stdio.h>
#include "function.c"
int main()
{
int a, b;
printf("Enter numbers a, and b: ");
scanf("%d %d", &a, &b);
printf("Here is ur answer: %d", random_fun(a, b));
return 0;
}
And here is my makefile:
OBJS = main.o function.o
program: $(OBJS)
$(CC) -o $@ $?
clean:
rm $(OBJS) program
Whenever I try type make, I get the following error:
duplicate symbol _random_fun in:
main.o
function.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
make: *** [program] Error 1"
Not sure what I am doing wrong. I can compile each code separately and main works. I was getting the same error for another project I was working on, so I tried with a very simple case involving only these 2 C files, and I get the same issues. I am fairly new to makefiles and what not, so bear with me if I am making a stupid mistake.