Undefined Symbol Errors

Hey, I am trying to compile the following code and whenever I try to do so it gives me a series of Undefined Symbol Errors like this:

Undefined first referenced
symbol in file
sDate::print() /var/tmp/ccUL1cIa.o
sDate::tomorrow() /var/tmp/ccUL1cIa.o
sDate::addDays(int) /var/tmp/ccUL1cIa.o
sDate::yesterday() /var/tmp/ccUL1cIa.o
sDate::read() /var/tmp/ccUL1cIa.o
sDate::sDate() /var/tmp/ccUL1cIa.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

There are no warnings. I believe it's a linking problem of some sort but I can't figure out what's wrong for the life of me. The code is only 3 files:


#ifndef DATE_H
#define DATE_H

Blah blah blah, the code for Date.h, just a class declaration; yes there is a semi-colon after the declaration; month, day, year, and the int array mday (which is static and const) are declared private, all member functions are declared public.



#include "Date.h"
using namespace std;

Blah blah blah, all member functions defined, all of them with sDate:: before the name of the function.


#include "Date.h"
using namespace std;

main() creates an sDate "day" and tests all member functions using it.

I compile using g++ in Unix with the following commands:

g++ -c Date.cc (works)
g++ Date_driver.cc (outputs undefined symbol errors)

When I include Date.cc instead of Date.h in the driver file the compiler doesn't output undefined symbols for all of the member functions, but it outputs an undefined symbol for "mday" (the array declared in the .h).

I cannot figure out what is wrong for the life of me. I have tried like everything. If anyone has any suggestions of something to try I'd appreciate it.


  • A typical cause of strange linker errors like this is variable definitions in the h-file. There is [italic]never[/italic] a reason to allocate variables inside the h-file.

    If you need a static const array, put it in the .c file. If it needs to be accessed outside the .c file, make (inline) wrapper functions to access it through.
  • Yeah, thanks, I figured it out. Problem with a code the professor provided that I just overlooked 900 times. Variable was defined in the .h instead of the .cc.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!