C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Undefined Symbol Errors Posted by shahkalukaking on 8 Mar 2009 at 7:14 PM
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:

Date.h:

#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.

#endif

Date.cc:

#include "Date.h"
#include <iostream>
using namespace std;

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

Date_driver.cc:

#include <iostream>
#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.
Report
Re: Undefined Symbol Errors Posted by Lundin on 9 Mar 2009 at 5:53 AM
A typical cause of strange linker errors like this is variable definitions in the h-file. There is never 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.
Report
Re: Undefined Symbol Errors Posted by shahkalukaking on 9 Mar 2009 at 9:45 AM
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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.