PDA

View Full Version : Leaning C++ and getting strange compile error


Ryan
2007-07-27, 23:14
A few days ago I borrowed a book on C++ from a friend and it's been going pretty well. It's intended audience is programmers who want to learn C++, but it's the only book I have access to so I figured I could at least give it a shot. Everything was working until I got to the first "project" in the book. I've never used a compiler much, as my only experience is a computer science class taught in Java, but I read through the options in the man file and I seemed to grasp at least the basics.

Or so I thought.

I'm trying to compile a file and keep getting a strange error that I don't understand.

Powerbook:~/Programming/C++/Employee Ryan$ g++ Employee.cpp -o Employee.o
/usr/bin/ld: Undefined symbols:
_main
collect2: ld returned 1 exit status

I have *no* clue what that means. I can't find any syntax errors, and it was all copied verbose out of the book anyways.

The project is an employee records system, intended to give you a crash course in C++. So far, I have three files: Employee.cpp, Employee.h and EmployeeTest.cpp. Any ideas?

(The book is Professional C++ by Solter and Kleper, BTW)

AsLan^
2007-07-28, 00:23
Try compiling them all at the same time and generate one binary.

Like this: g++ Employee.h Employee.cpp EmployeeTest.cpp -o Employee

ShadowOfGed
2007-07-28, 00:41
If you're compiling object files only, you need to compile with "-c" to indicate that you only wish to compile; otherwise, gcc and g++ try to link all of the objects into an executable. Like this:

g++ -c Employee.cpp -o Employee.o

To create a valid executable, obviously, you need to define main(). Alternatively, you can compile all of your code in one pass as AsLan^ suggests. For large projects, that's not the best option; compile each file to an object, and then link all the objects together when you're done.

You'll note that it's /usr/bin/ld (the linker) that is failing, not g++ (the compiler).

When you want to get a running executable, you'd run:

ld -o EmployeeBinary Employee.o EmployeeTest.o

Assuming those are the names of your object files. Hope this helps.

:)

Ryan
2007-07-28, 01:24
Try compiling them all at the same time and generate one binary.

Like this: g++ Employee.h Employee.cpp EmployeeTest.cpp -o EmployeeThanks, that did it! :)

AsLan^
2007-07-28, 01:43
Np, you're going to want to revisit ShadowOfGed's advice though after your programs start to get more complicated.

But by that time, you should really start using Make files ;)

Ryan
2007-07-28, 03:06
Okay, different files, new error.

Powerbook:~/Programming/C++/Employee Ryan$ g++ -o Database Database.cpp Database.h DatabaseTest.cpp
/usr/bin/ld: Undefined symbols:
Records::Employee::getIsHired()
Records::Employee::getLastName()
Records::Employee::setLastName(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)
Records::Employee::getFirstName()
Records::Employee::setFirstName(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)
Records::Employee::getEmployeeNumber()
Records::Employee::setEmployeeNumber(int)
Records::Employee::hire()
Records::Employee::display()
Records::Employee::Employee()
Records::Employee::fire()
Records::Employee::promote(int)
Records::Employee::setSalary(int)
collect2: ld returned 1 exit status


I tried Shadow's method of breaking it down into separate steps of compiling and then linking as well, with the same error. The problem appears to be with the linker, but I don't know how to diagnose it.

ShadowOfGed
2007-07-28, 03:15
The database depends on the Employee record, so you've got to link it with that object too (Employee.o).

You probably need to find a website that better explains the ins-and-outs of compiling and linking executables. Probably the Wikipedia articles on Object files (http://en.wikipedia.org/wiki/Object_file), Linkers (http://en.wikipedia.org/wiki/Linker), and Compilers (http://en.wikipedia.org/wiki/Compiler).

Of course, Kickaha will do a much better job explaining it. I know what's happening, I'm just too zapped right now to explain it clearly... :\

AsLan^
2007-07-28, 05:05
Try putting the header file first.

Like this: g++ Database.h Datebase.cpp DatabaseTest.cpp -o Database

chucker
2007-07-28, 05:55
Records::Employee::setLastName(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)

C++ code makes baby chucker cry. :(

Ryan
2007-07-28, 10:43
The database depends on the Employee record, so you've got to link it with that object too (Employee.o).

You probably need to find a website that better explains the ins-and-outs of compiling and linking executables. Probably the Wikipedia articles on Object files (http://en.wikipedia.org/wiki/Object_file), Linkers (http://en.wikipedia.org/wiki/Linker), and Compilers (http://en.wikipedia.org/wiki/Compiler).

Of course, Kickaha will do a much better job explaining it. I know what's happening, I'm just too zapped right now to explain it clearly... :\You're right, I need some kind of "intro to compiling." I'll read through those links today.

klep
2007-08-06, 14:48
You're right, I need some kind of "intro to compiling." I'll read through those links today.

Ryan -- if you're downloading the code from the site, most/all of the examples should compile using "g++ *.cpp". As mentioned previously, you were missing some of the cpp files for the example you were looking at.

Hope that helps.

Scott