-1

This is a particularized question from mingw32/bin/ld.exe ... undefined reference to [class] ... collect2.exe: error: ld returned 1 exit status

There is a user-defined class inside MyClass.hpp:

class MyClass
{
    public:
        MyClass(const string& className); 

        ~MyClass() {cout << "Destructor definition instead of g++ default one?";} ; 
        ...

and you try to construct an object out of it in the main file:

#include "MyClass.hpp" //in the same directory
...
int main()
{
...
MyClass myClassObj = MyClass(myName); //here is the linker problem
...
return 0;
}

The error:

c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\....:Main.cpp:(.text+0x124c): undefined reference to `MyClass::~MyClass()'
collect2.exe: error: ld returned 1 exit status

There are two questions: 1. How can I build a Makefile or which g++ command can I use to have the correct linkage of MyClass into Main? 2. How can g++ use this own default destructor (in this case I did not defined it at all, still not working). Or, if I need to define one myself, how is the best way to do it?

simple compilation command:

g++ -o MyProgram.exe Main.cpp -Wall

I also tried the Makefile from : mingw32/bin/ld.exe ... undefined reference to [class] ... collect2.exe: error: ld returned 1 exit status

And I checked the toolchain dependencies by following : Makefile: How to correctly include header file and its directory?

user3742309
  • 183
  • 2
  • 12
  • 1
    Sure your code compiles? The semicolon is off in this line: `~MyClass() {cout << "Destructor definition instead of g++ default one?"} ;` It should be: `~MyClass() {cout << "Destructor definition instead of g++ default one?";}` – Taron Jan 24 '20 at 11:30
  • sorry mistyping, I corrected – user3742309 Jan 24 '20 at 11:33
  • 1
    Sorry I cannot reproduce your problem. Try to come up with a minimal compilable example which produces the error. – Taron Jan 24 '20 at 11:37
  • Ok, I will come with a minimal example today, thank you – user3742309 Jan 24 '20 at 11:40
  • 1
    As you can see here: https://onlinegdb.com/rkbWuIu-L it runs and compiles fine. – Taron Jan 24 '20 at 11:45
  • In your given example, you are not defining your constructor, which should result in a linker-error. Simply adding `{}` could solve it. Might be worth a try. – Frederik Juul Jan 24 '20 at 13:09

3 Answers3

0

Do you have MyClass constructor and destructor definition (like specified) in your header file? Do you use Makefile or only try to execute g++ via terminal? Linker needs compiled object of MyClass. If you compile main.cpp to entire executable it should contain all declared definitions.

artaxerx
  • 244
  • 1
  • 3
  • 11
0

I had the same problem as you. Try to move your constructor and destructor definition from the cpp into the header file. This way, the linkage is well done only by running the simple g++ command that you mentioned.

Try:

MyClass(const string& className){ _className=className }; 

If the definition is inside the cpp file, I get the same error as you have.

kaileena
  • 121
  • 9
  • 1
    actually this works, indeed, but why does not the compiler find the constructor definition if I am having it inside the cpp file? Currently I have the definitions inside the hpp file. – user3742309 Jan 25 '20 at 11:42
  • 1
    This is a workaround, not a solution. Making everything inline creates severe build and maintenance headaches in non-trivial projects. – Pete Becker Sep 20 '22 at 18:04
0

it seems like you got your answer , but for the good habit you have to define your class in yourclass.hpp and the implementation in the cpp file and for the main you have to initiate the class object befor the main function to get the correct link file even if you got multiple files in your project. I hope that it will be useful for you.

  1. class defintion:

    /*-------------------Myclass.h-------------------------*/
    class Myclass{
    
    private:
     //private member just for the exapmle
      int number;
    
    public:
    //Default constructor
      Myclass();
    //Parametrized constructor for any parametre it's just an example
      Myclass(const int n);
    //Destructor
      ~Myclass();
    //here you define your class's functions as it's public or private ...
      void Mymethod();
    
    };
    
  2. class implementation:

    /*-------------------Myclass.cpp-------------------------*/
    
    #include <iostream>
    
    #include "Myclass.h"
    
    //constructor
    Myclass::Myclass()
    
    {
       std::cout << " this is the default constructor"<< std::endl;
    }
    
    Myclass::Myclass(const int n)
    {
        std::cout << " this is the parametrized constructor"<< std::endl;
        number = n;
    }
    //destructor
    Myclass::~Myclass()
    {
        std::cout << " this is the default destructor"<< std::endl;
    }
    
    
    //method 
    
    void Myclass::Mymethod()
    {
        std::cout << " this is the function to perform within this class" 
        <<std::endl;
    }
    
  3. main program:

    #include "Myclass.h"
    
    //default constructor
    Myclass _class;
    
    //parametrized constructor
    Myclass _pclass(10);
    //here you must have the correct link to enter the main function
    int main (int argc, char** argv)
    {
        //here you call the desire function from your class 
        _class.Mymethod();
    }
    
  4. compilation

    g++ Myclass.cpp Mymain.cpp -o Myexec
    
Islem
  • 1
  • 3