Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Undefined reference to function" error

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().

like image 313
Tesla Avatar asked Jun 30 '12 20:06

Tesla


1 Answers

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.

like image 102
aps2012 Avatar answered Sep 28 '22 11:09

aps2012