Undefined Reference when compiling multiple files

Asked

Viewed 942 times

1

I’m learning C++, and I have a question about creating classes in different files. I created the header with prototypes of functions, then as member functions in another cpp file.

When I go to test the files next to a main I have the following error:

undefined reference to GradeBook::GradeBook(std::string) linha 14
undefined reference to GradeBook::getNomeCurso() linha 16

This is the header with the gradebook class : gradebook. h

#include <iostream>
#include <string>

using namespace std;

class GradeBook
{
public:
    GradeBook( string );
    void setNomeCurso(string);
    string getNomeCurso();
    void mostrarMensagem();

private:
    string nome_curso;
};

This is the file called gradebook.cpp with the member functions in it:

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

using namespace std;

GradeBook::GradeBook( string z ){
   setNomeCurso(z);
}

void GradeBook::setNomeCurso( string nome ){
    nome_curso = nome;
}

string GradeBook::getNomeCurso(){
    return nome_curso;
}

void GradeBook::mostrarMensagem(){
    cout << "Bem vindo ao Livro de " << getNomeCurso() << "!" << endl;
}

And this is the test.cpp file with the Main function:

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

using namespace std;

int main() {
    string curso;

    cout << "Entre com o nome do curso : ";
    getline(cin, curso);
    cout << endl;

    GradeBook Livro( curso );

    cout << "Bem vindo ao curso de " << Livro.getNomeCurso() << endl;

}
  • 3

    Which command was used to compile?

  • 1

    Yeah, it looks like you didn’t compile/link the gradebook.cpp along with the.cpp test.

  • 1

    You need to generate the compiled object of the class before compiling the main program. Are you using Windows or Linux? Any IDE? Compiling at Hand?

  • I am using codeblocs in windows, I will give a search on compiling the object, thank you

3 answers

4


You must compile each file .cpp in an object .the. In sequence link the objects in a final binary. Your problem is because you only compiled the main file and at the time of creating the executable some functions were missing (the gradebook.cpp). Assuming you are using GCC, do the following:

g++ -c main.cpp
g++ -c gradebook.cpp
g++ main.o gradebook.o -o programa

Or, simply:

g++ main.cpp gradebook.cpp -o programa

Recommended reading: Creating your own header file (although this is about C, not C++)

  • I am using codeblocks in windows, I will give a search on compile in . o, thank you.

4

Assuming you are using GCC or some compiler with similar interface, do, on the command line:

$ g++ -c gradebook.cpp -o gradebook.o
$ g++ Main.cpp gradebook.o -o <nome_do_programa>

You can make your life easier by using GNU Make (for Linux) or tools like Cmake that are compatible with Windows and do:

Makefile:

main: Main.cpp gradebook.o
    g++ $^ -o <nome_do_programa>

%.o: %.cpp
    g++ -c $^

On the command line:

$ make main

0

Here it seems to be working.

g++ Main.cpp gradebook.cpp

Browser other questions tagged

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