“Undefined reference to function” error

2019-06-16 05:55发布

问题:

I am having some trouble compiling a few files using headers. Here is a breakdown of my code:

file1.c

#include "header.h"
int main() {
    func1();
    return 0;
}

file2.c

#include "header.h"
void func1() {
    ... function implementation ...
}

header.h

void func1();


The error I am getting is:

In function 'main':
undefined reference to 'func1'

Note: I am just using a simple breakdown of how my 3 files are set up. I need to get this to work with the 3 files. I am setting/including everything properly? I need to use this set up, but I am just unsure how file.c gets reference to the actually implementation of func1().

回答1:

If the error is an undefined reference to func1(), and there is no other error, then I would think it's because you have two files called header.h in your project and the other copy is being included instead of your copy with the declaration of func1().

I would check the include paths for your project and make sure that the header.h with your declaration of func1() is being included first.