tags:

views:

246

answers:

4

Got some c code I'm working on, and it looks like it should work. When I try to like the object files, I get an error saying "undefined reference to outputBus" and so on for each of them in the getLine function in main. I've tried it with and without the ampersand before the function names. compiling with "gcc -ansi. what am i doing wrong?

typedef void(*DualOutput)(const int, const int);
typedef void(*TripleOutput)(const int, const int, const double);

void   passenger(node**, node**, itemtype*);
double bus(node**, node**, itemtype*);
int    getLine(itemtype*, DualOutput, DualOutput);
void   getLines(node**, node**, DualOutput, DualOutput, TripleOutput);
void   outputBus(const int, const int);
void   outputPeople(const int, const int);
void   outputTotal(const int, const int, const double);

int main(int argc, char **argv){
  node *head = NULL;
  node *tail = NULL;

  getLines(&head, &tail, outputBus, outputPeople, outputTotal);
  return 0;
}
+7  A: 

Do those functions have bodies anywhere in your code? All of those lines before the main function are only function declarations (aka prototypes). You actually have to create and implement the function somewhere.

A function declaration is a promise to the compiler that the function will exist somewhere later in the code (or in another object file), so it doesn't complain. Then the linker sees that you broke your promise and gets upset.

Tyler McHenry
+1 love the promise analogy
David Zaslavsky
+1 for upsetting th linker :)
qrdl
+1  A: 

"Undefined reference" is a linker error that means the linker cannot find the implementation of the method. If it's defined in a separate source file, are you compiling and linking both of them together? If it's in a library, are you linking to the library (with gcc's -lfoo option for libfoo)?

Harper Shelby
+1  A: 

Some "is it plugged in?" type questions to get you started:

  • Did you actually implement the functions outputBus(), outputPeople(), and outputTotal() anywhere?

  • If so, was that module included in the link?

As written, the output functions are declared but not defined. Without a definition in some object file included in the link they are indeed undefined references. The linker is trying to tell you that you need to define those functions.

RBerteig
+1  A: 

You've declared functions but don't implement them anywhere. This just declares a function:

void   outputBus(const int, const int);
Jay