I have two source files:
Source FIle 1 (assembler.c):
#include "parser.c"
int main() {
parse_file("test.txt");
return 0;
}
Source File 2 (parser.c):
void parse_file(char *config_file);
void parse_file(char *src_file) {
// Function here
}
For some reason, when compiling it is giving me the following error:
duplicate symbol _parse_file in ./parser.o and ./assembler.o for architecture x86_64
Why is it giving me a duplicate symbol for parse_file? I am just calling the function here… No?
First off, including source files is a bad practice in C programming. Normally, the current translation unit should consist of one source file and a number of included header files.
What happens in your case is that you have two copies of the
parse_filefunction, one in each translation unit. Whenparser.cis compiled to an object file, it has its ownparse_filefunction, andassembler.calso has its own.It is the linker that complains (not the compiler) when given two object files as an input, each of which contains its own definition of
parse_file.You should restructure your project like this:
parser.h
parser.c
assembler.c