0

So I am using Atom for a while but using it for the time for C++. I am getting a compilation error when I run the

Object Oriented Coding.cpp file

cpp file if I include my own header file for some reason. The cpp code is:

/* Object Oriented Coding.cpp file */
#include <iostream>
#include "Cat.h" // Our madeup header file
using namespace std;

int main(){

  speak();
  return 0;
}

The header file is:

/* Cat.h file with my prototypes */
#ifndef CAT_H_
#define CAT_H_

void speak();

#endif

The function file is:

#include <iostream>
using namespace std;

void speak(){
 cout << "Meow Meow ..." << endl;
}

I am using the gcc-make-run package to compile my c++ code. Also all of the above files are in the same folder

The compilation error is:

C:\Users\JAPNIT~1\AppData\Local\Temp\ccIb2JOh.o:Object Oriented Coding.cpp:(.text+0xe): undefined reference to `speak()' collect2.exe: error: ld returned 1 exit status

Error

  • I have added the error image link – Japnit Sethi Mar 25 '20 at 22:23
  • 1
    Your code compiles successfully. You're issue is that the _linker_ is unable to produce an executable since it can't find the definition for `speak`. Try passing **both** the file with your `main` function and the file that defines `speak` to `g++`. – Brian61354270 Mar 25 '20 at 22:27
  • 1
    The errors need to be included _as text_, not images. See [ask]. – 1201ProgramAlarm Mar 25 '20 at 22:28
  • @Brian Is there no way I can write functions in a separate cpp file and link it to main in atom ? – Japnit Sethi Mar 25 '20 at 22:32
  • @JapnitSethi You most certainly can, just not with the `g++` command your are currently using. See [this related question](https://stackoverflow.com/questions/6532400/linking-files-in-g) – Brian61354270 Mar 25 '20 at 22:34

0 Answers0