PDA

View Full Version : Newbie with C++ Compile/Link problem




PeteY48
Jul 5, 2008, 08:59 PM
I am starting a course on C++ and am having problems with compiling/linking with OSX 10.5.4 using BBedit for writing and g++ for compiling/linking.

My core program is main.cpp consisting of:

#include <iostream>
using std::cout;
using std::endl;

#include "GradeBook.h"

int main()
{
// create two gradeBook objects
GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
GradeBook gradeBook2( "CS102 Data Structures in C++" );

// display initial value of courseName for each GradeBoook
cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
<< "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
<< endl;

return 0;
}

and the GradeBook.h file is:

#include <string>
using std::string;

// GradeBook class definition
class GradeBook
{
public:
GradeBook( string name );
void setCourseName( string name );
string getCourseName();
void displayMessage();
private:
string courseName;
};

When I try to compile link with "g++ -Wall main.cp -o Chapter3"

I get the following:

Undefined symbols:
"GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
_main in cc2oAZuH.o
_main in cc2oAZuH.o
"GradeBook::getCourseName()", referenced from:
_main in cc2oAZuH.o
_main in cc2oAZuH.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

All of the files are in the same directory and the g++ is run from the same directory. I understand the errors if the "#include "GradeBook.h" were not included in the main.cpp file, but it is. I am very new to this compile/link process, and would appreciate any and all advice to solve my problem.



iSee
Jul 5, 2008, 10:36 PM
What's missing is the file that holds the bodies of the Gradebook functions. Gradebook.h only declares them. But there is no definition.

Do you have a Gradebook.cp (or Gradebook.cpp, or whatever)? If so, try this line:

g++ -Wall main.cp Gradebook.cp -o Chapter3

PeteY48
Jul 5, 2008, 11:25 PM
I did/do have the GradeBook.cpp file. I was unaware that I needed to have it called out in the g++ command line. Adding it to the command line solved the problem and the program compiled correctly.

If I have a project with 32 classes in C++, will I need to have them all spelled out in the g++ command line? Or is their an easier way to execute it with perhaps a text file listing all the class files that I can call in the compile command line?

Cromulent
Jul 5, 2008, 11:39 PM
If I have a project with 32 classes in C++, will I need to have them all spelled out in the g++ command line? Or is their an easier way to execute it with perhaps a text file listing all the class files that I can call in the compile command line?

Do a Google search for "GNU Make Tutorials" and that should point you in the right direction.

lee1210
Jul 5, 2008, 11:40 PM
make (http://unixhelp.ed.ac.uk/CGI/man-cgi?make) is your friend.

-Lee