0

I'm doing this using a sublime text editor and GNU Compiler.... And I have created this in these three file at the same hierarchical level. Still don't why it showing .....

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe C:\Users\BIVASH~1\AppData\Local\Temp\cc1guWsB.o:Class.cpp:(.text+0xc): undefined reference to `speak()'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\BIVASH~1\AppData\Local\Temp\cc1guWsB.o:Class.cpp:(.text+0x11): undefined reference to `jump()'
collect2.exe: error: ld returned 1 exit status```

Class .cpp

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

using namespace std;

int main()
{
    speak();
    jump();
    return 0;
}

Cate.h

#ifndef CAT_H
#define CAT_H
void speak();
void jump();
#endif

Cat.cpp


#include <iostream>
#include "Cat.h"
using namespace std;

void speak(){
    cout<<"mmmeeeeoooowwww!!!!";
}
void jump(){
    cout<<"jump jump jump !!!!!";
}
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • 1
    You must compile `cat.cpp` too and include it in the linker command. – Paul Ogilvie Jun 27 '20 at 11:57
  • Are you using Visual Studio Code? If so you did not configure your `tasks.json` properly. – drescherjm Jun 27 '20 at 12:29
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Adrian Mole Jun 27 '20 at 12:37

1 Answers1

1

When you want to use functions written on a different compilation unit, you have to link with the other unit's object file.

To do this, it's not sufficient to include the other unit's header, because it gives you only the declarations of the functions. In order to reach the definitions, you should compile both of the units together. For example, For foo.cpp and bar.cpp: g++ foo.cpp bar.cpp

In your case, just write:

g++ Class.cpp Cat.cpp
MrBens
  • 1,227
  • 1
  • 9
  • 19