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

Asked

Viewed 377 times

3

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

#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 through the inclusion of the header file gradebook.h. What I need to know is how to generate the object code of this class, GradeBook, to make available for the user’s use of it through the gradebook.h. How to do it by g++?

  • Don’t you know how to make the compiler generate the object? Or is it another question?

  • How to make the compiler generate the @bigown object

  • If I didn’t ask the question correctly, please edit it, I’m a beginner and maybe I didn’t understand correctly the operation of the interface and implementation of a @bigown class

  • I just wanted to be sure, you were prudent in putting as much information as possible, even more than I needed (but didn’t know yet), so I wanted to be sure what I was asking.

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.

  • 1

    Thank you very much for the attention, I noticed that you edited the reply and gave it all right @Joséx.

  • I don’t know if you’re remembering more you’ve helped me before with another question, it’s really cool to know that you’re always willing to help

  • @vanfilho the -o that he had put in the first version of his answer is to rename the output file. Ex,: g++ -o lib.o -c main.cpp compile the mail.cpp but will generate an object lib.o. Without the -c wouldn’t work because it would only change the file name, but it wouldn’t generate the object file, it would be a normal executable.

  • @vanfilho The separation of the interface of a class (in fact, the technical name is statement) and its implementation (the technical name is definition) is just a convention, not a requirement of the C++ language. Similarly, a source file can have as many classes, or functions, etc, the programmer wants. It’s all about organizing the source files of an application.

  • @Joséx. Now that I already have the object code as in fact I can make it available for the client of the class to use it?

  • Valeuuu! kkkk... This motivates me to learn the language more and more. See you next time! @Joséx.

Show 1 more comment

2

To generate the object is typically used:

g++ -c main.cpp

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

For C it works the same, just call gcc.

Documentation.

You might want to use the libtool to better manage these objects.

  • Thanks for the help also @bigown

  • This community of C++ programmers is really cool, thank you very much for the tip of libtool @bigown, I will try it, any doubt I come here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.