Newbie with C++ Compile/Link problem

Discussion in 'Mac Programming' started by PeteY48, Jul 5, 2008.

  1. PeteY48 macrumors newbie

    Nov 3, 2003
    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
    GradeBook( string name );
    void setCourseName( string name );
    string getCourseName();
    void displayMessage();
    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.
  2. iSee macrumors 68040


    Oct 25, 2004
    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
  3. PeteY48 thread starter macrumors newbie

    Nov 3, 2003
    Orange Cty, CA

    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?
  4. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    Do a Google search for "GNU Make Tutorials" and that should point you in the right direction.
  5. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX

Share This Page