9

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

Eitan T
  • 32,660
  • 14
  • 72
  • 109
Tesla
  • 822
  • 2
  • 13
  • 27
  • 2
    what compiler are you using ?? can you provide your compiler statement? – mathematician1975 Jun 30 '12 at 20:21
  • 3
    If you're using `gcc -o program file2.c file1.c`, change this to `gcc -o program file1.c file2.c`. Order is important. –  Jun 30 '12 at 20:28
  • Hmm, your right that this should work under normal gcc compiling. Problem is, this issue for more is from a very big project, with hundreds of files. But the structure of the problem is the same. I just thought maybe I wasn't including stuff properly, but I guess that's not the case. – Tesla Jun 30 '12 at 20:41
  • I just tried this with gcc -- gcc -o test file2.c file1.c -- using file1.c and file2.c in both positions. Something else is afoot, I think. I'm on Ubuntu 10.04 gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1) – octopusgrabbus Jun 30 '12 at 20:53
  • @H2CO3 - WHAT?!?!? How in the world is order important for this? Both orders work fine for me. I don't get the undefined reference either way. – phonetagger Jun 30 '12 at 20:56
  • @phonetagger GCC looks for symbols in the order they're needed to be resolved. If you specify a wrong order, you may get "undefined reference" errors. –  Jun 30 '12 at 21:03
  • Yes I am, but I'm not sure how much my environment is a result of this. The interaction is between 3 files, as I described, but the project I'm working on is for school and is really big (we're implementing a simulated OS based on Harvard's OS/161 system). Still, I am not sure why it cannot find reference to the function. – Tesla Jun 30 '12 at 21:07
  • @H2CO3 - I'd like to see an example of what you're saying, or perhaps a link to some page that describes it. The error Tesla's seeing is a linker error, not a compiler error. At the linking stage, functions in one file should be able to call functions in another file, and vice-versa. I'm pretty sure the linker should have no problem doing its job regardless of the order the files were specified. – phonetagger Jun 30 '12 at 21:16
  • @Tesla - Since your project is really big, I'm assuming you're surely using a makefile or have a Visual Studio solution+project setup. Is it possible you added a new source file & didn't include it in your makefile/project file list? – phonetagger Jun 30 '12 at 21:19
  • @phonetagger I know what the difference between linker and compiler errors is. –  Jun 30 '12 at 21:19
  • 1
    @H2CO3: AFAIK, the order matters only for libraries. I.e. libs (`-lnameoflib`) should be specified after the sources using symbols from them, and if there are inter-lib dependencies, then order of libs matters too, or the grouping options must be used (linker then goes in a loop for the group, until all references are satisfied or no more can be resolved.) – mity Jun 30 '12 at 21:28
  • @H2CO3 Order matters only for linking static libraries. – Seth Carnegie Jun 30 '12 at 23:26

1 Answers1

4

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.

aps2012
  • 1,602
  • 12
  • 8
  • This is soooooo old, but I am running into the issue and I'd appreciate help. The basic set up is the same as this question, except I am sure that correct header is being included, because some functions do not give undefined references and some do. It's just weird, and if I jeopardize the header then the code "correctly" doesn't compile. – Vahagn Tumanyan Mar 17 '17 at 15:34