0

I'm getting an undefined reference error when compiling the code below.

Here is main.cpp:

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

using namespace std;

int main()
{
    Person person1;

    cout << "Age is: " << person1.getAge() << endl;
    cout << "Name is: " << person1.getName() << endl;

    system("PAUSE");

    return 0;
}

Here's Person.cpp:

#include "Person.h"

#include <string>
using namespace std;

int Person::getAge()
{
  return age;
}

string Person::getName()
{
  return name;
}

Here's Person.h:

#ifndef PERSON_H
#define PERSON_H

#include <string>
using namespace std;

class Person
{
   private:
    int age = 25;
    string name = "Jack";

  public:
    int getAge();
    string getName();
};

#endif

Error:

> Executing task: & 'C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe' -g d:\Desktop\howdy\main.cpp -o d:\Desktop\howdy\main.exe <

C:\Users\MAC\AppData\Local\Temp\ccbY6HmM.o: In function `main':
d:/Desktop/howdy/main.cpp:32: undefined reference to `Person::getAge()'
d:/Desktop/howdy/main.cpp:33: undefined reference to `Person::getName[abi:cxx11]()'
collect2.exe: error: ld returned 1 exit status
The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command & 'C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe' -g d:\Desktop\howdy\main.cpp -o d:\Desktop\howdy\main.exe" terminated with exit code: 1.

I'm using VS Code with GCC (MinGW). I tried running the build task with g++ and gpp. I checked the c_cpp_properties.json. Also, I read and followed the docs here: https://code.visualstudio.com/docs/cpp/config-mingw

❯ g++ --version
g++.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0

❯ gdb --version
GNU gdb (GDB) 8.1

If I put the class specification and implementation in main.cpp (instead of including it from Person.h), it compiles without issues. This makes me think it's a compiler/linker issue. Any ideas?

night_train
  • 5
  • 1
  • 5
  • The c_cpp_properties.json is used for intellisense syntax highlighting and is not used for compiling - the file you want to edit is tasks.json – Jerry Jeremiah Jul 06 '21 at 05:13
  • I think this is the answer: https://stackoverflow.com/questions/47665886/vs-code-will-not-build-c-programs-with-multiple-ccp-source-files – Jerry Jeremiah Jul 06 '21 at 05:15

1 Answers1

1

It's because you are not including Person.cpp as a compiling parameter. You need to add Person.cpp aswell as main.cpp file

  • Interesting! Following your suggestion, I included Person.cpp in main.cpp and it compiled without issues so that's great! :) But it makes me wonder -- how come? I've been pouring over Gaddis' textbook and online tutorials for class implementations with headers and none of them include the .cpp file in the main.cpp. They all work by just including the header in the implementation file and in the client program... – night_train Jul 06 '21 at 04:41
  • @night_train He didn't mean to #include person.cpp into main.cpp - you should never #include one source file in another (only #include header files) What he meant is that both main.cpp and person.cpp have to be compiled into the executable. Your log shows: `Executing task: & '...\g++.exe' -g d:\Desktop\howdy\main.cpp -o d:\Desktop\howdy\main.exe` which shows that main.cpp is being compiled and person.cpp is not. If you are using vs code you need to edit tasks.json so that both source files are there. – Jerry Jeremiah Jul 06 '21 at 05:11
  • Thanks @JerryJeremiah and @ErnestoAlvarez ! Editing tasks.json solved the problem. For anyone in the future, I specifically changed one of the `args` from `"${file}"` to `"${fileDirname}\\*.cpp"` – night_train Jul 06 '21 at 13:41