Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

PeteY48

macrumors newbie
Original poster
Nov 3, 2003
6
0
Orange Cty, CA
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

macrumors 68040
Oct 25, 2004
3,540
272
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

macrumors newbie
Original poster
Nov 3, 2003
6
0
Orange Cty, CA
Thanks!

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

macrumors 604
Oct 2, 2006
6,816
1,100
The Land of Hope and Glory
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.