How to generate an .o of the implementation of a .cpp class without the main function in g ++?

3

I would like to compile the implementation of a related class to only generate an object code of it. This is the class implementation:

#include <iostream>
#include "gradebook.h"

using namespace std;

GradeBook::GradeBook(string name_course_param)
{
    setCourseName(name_course_param);
}

void GradeBook::setCourseName(string name_course_param)
{
    courseName = name_course_param;
}

string GradeBook::getCourseName()
{
    return courseName;
}

void GradeBook::displayMessage()
{
    cout << "Welcome to the grade book for\n" << getCourseName()
      << "!" << endl;
}

I know I need the interface, as you can already see I'm already using the header file gradebook.h . What I need to know is how to generate the object code of this class, GradeBook , to make it available for user use of it through gradebook.h . How to do this through g ++?

    
asked by anonymous 17.09.2016 / 22:15

2 answers

2

g++ -c xxx.cpp

This command generates the file xxx.o .

To compile, use g++ -o yyy main.cpp xxx.o . This command compiles main.cpp and link-edits with xxx.o , creating the executable yyy .

    
17.09.2016 / 22:25
2

To generate the object typically is used:

g++ -c main.cpp

Where -c avoids linking the executable and generates an object.

For C works the same, just call gcc .

Documentation .

You may want to use libtool to manage these objects in a better way.

    
17.09.2016 / 22:25