p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional C++ (http://p2p.wrox.com/forumdisplay.php?f=158)
-   -   [Linker error] undefined reference (http://p2p.wrox.com/showthread.php?t=30242)

philArmstead June 8th, 2005 01:24 PM

[Linker error] undefined reference
So, I've been leafing though my copy of Professional C++ and working through it, but whenever I run some code which involves classes I get the error "[Linker error] undefined reference to `Records::employee::employee()'"

I have recieved this error by copying the code from the book, verbatim, and from running the code I downloaded from this website. I have compiled the code on Windows XP with DEV C++ from Bloodshed.net, and on Mandrake 10.2 with the built in C++ compiler.

Any ideas?

klep June 20th, 2005 12:36 PM

Hi there,

A few possiblities:

* The error has employee as lower-case. Employee is a class, so in the book we capitalized it (Employee). Is it capitalized in all locations in your code?

* Did you download the code from this site to avoid a transcription error? You can get the sample code from the book at http://www.wrox.com/go/downloads

* Are you sure that you created a C++ project and not a C# or C project? This seems kind of unlikely, but it's worth checking out.

* Are you sure that all of the relevant source files are included with the project?

Please let us know if that helped or if you have resolved the problem some other way.

Scott J. Kleper

Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)

philArmstead June 30th, 2005 06:37 AM

I downloaded the code samples from the site, unzipped the chapter 1 samples, and and double clicked Employee.h and EmployeeTest.cpp, which are located in the EmployeeDB folder. They opened up in Dev C++ so I removed the comments from the EmployeeTest.cpp file, (because the code was 'commented-out'), then I tried to compile it. Nope :(

Screenshots of the errors can be found on these two screenshots:

Thinking that the comments on the EmployeeTest.cpp might have been there for a reason, I also tried compiling Employee.cpp (which also relies on Employee.h), but I got the exact same errors.

klep July 6th, 2005 01:48 AM

Thank you for the additional info.

I downloaded devcpp and reproduced the error (I hadn't ever actually used this IDE before -- it's nice!)

The error you get is actually correct -- the IDE is compiling the EmployeeTest class. Since this class #includes "Employee.h", it gets the declaration for the Employee class and all of its associated methods. However, when the linker tries to generate the application, it can't find the *definition* of the Employee class and its methods. It knows the names, but never saw or compiled the implementations.

The solution is to add the Employee.cpp file to the project. From your description, it sounds like maybe you didn't create a project in Dev-C++. Doing so is necessary because otherwise it will just try to compile and run a single file. Here are the steps to create the project and run the example:

1) File -> New -> Project...
2) Choose "Console Application" and click OK
3) Save the project somewhere
4) The project starts you off with a main.cpp. In the "project" tab in the left sidebar, right click on 'main.cpp' and select "Remove File". The project is now empty.
5) Right click on Project1 (or whatever your project is called) under the 'project' tab in the left sidebar and select "Add to Project".
6) Navigate to the EmployeeDB folder and select Employee.cpp and click OK
7) Repeat step 6 selecting EmployeeTest.cpp
8) Execute -> Compile and Run

At this point, you'll see the files compile, link, and the console window should come up and disappear. That's because it runs so fast that you don't have a chance to see the output. To see the output, you can either run in debug mode, stepping through each line, or you can run the .exe file from a command window. My executable wound up in C:\Dev-Cpp\Project1.exe

Also, the reason that the main() is commented out in EmployeeTest is because the *real* program for EmployeeDB is found in UserInterface.cpp. I didn't want the two mains to collide but I wanted to leave the test code in for reference. I apologize if that was confusing. As long as you're just using Employee.cpp and EmployeeTest.cpp, go ahead and uncomment the code.

Hope this helps!


Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)

philArmstead July 8th, 2005 09:09 AM

Thank you very much, that solved the issue perfectly.

inkognito October 8th, 2005 10:27 AM


I get the same error as described by thread starter. I am using Mandriva (former Mandrake) and the "built in" C++ compiler g++ version 3.4.3.

The problem is the same; only the declaration of the Employee constructer is included in the EmployeeTest file, so it can't find the definition of the Employee constructor (which is in the Employee.cpp file. If I include this file in the EmployeeTest file instead of the header file, it works fine.).

So the question is, how do link the program properly with this c++ compiler?

inkognito October 8th, 2005 11:46 AM


After a bit of 'try and fail', I got it to work.
I compiled the Employee.cpp file first without linking it:

g++ -c Employee.cpp
Then I compiled the EmployeeTest.cpp and included the Employee.o file for the linker:

g++ EmployeeTest.cpp Employee.o -o test
Worked like a charm.

klep October 9th, 2005 05:40 PM

Glad you got it to work, although the separation into two steps shouldn't be necessary. On Linux, the following command line successfully compiles and links the example for me:


g++ Employee.cpp EmployeeTest.cpp -o test
Of course, I had to uncomment main() in EmployeeTest.cpp, but it sounds like you've already done that.

Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)

All times are GMT -4. The time now is 09:13 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.