0

Error when compiling my main file & external Class file using VScode.

file structure:

project/
 --main.cpp
 --MyClass.cpp
 --MyClass.h

MyClass.h

#ifndef MY_CLASS_H
#define MY_CLASS_H

class MyClass
{
public:
    MyClass();
};

#endif

MyClass.cpp

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

MyClass::MyClass()
{
    std::cout << "MyCLass is created" << std::endl;
}

main.cpp

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

int main()
{
    MyClass myClass;
    return 0;
}

Compile error:

main.cpp:9: undefined reference to `MyClass::MyClass()'

Evan
  • 2,327
  • 5
  • 31
  • 63
  • `g++ -Wall -Wextra -pedantic -Wshadow -std=c++17 -Ofast MyClass.cpp -o main main.cpp` -- Works fine. `./main` yields `"MyCLass is created"`. How are you compiling? – David C. Rankin Jul 03 '22 at 00:45
  • I am just using `g++ main.cpp` in the terminal – Evan Jul 03 '22 at 00:53
  • 1
    Yep, that's the problem, you need `g++ main.cpp MyClass.cpp` so both source files are compiled (with the addition options as desired) `-o main` says output executable named `main`, `-Wall -Wextra -pedantic -Wshadow` enable full warnings and warn on shadowed variables, `-std=c++17` the language standard to compile with and `-Ofast` your optimization. (could be `-O0` for none through `-O3` for highest with strict standard compliance or `-Ofast` even more optimization even if you bend the standard producing faster code that does the same thing as if the standard had been followed) – David C. Rankin Jul 03 '22 at 01:35
  • 1
    The proper solution is to edit your `tasks.json` file so VSCode builds all of your files. Including a `.cpp` is a bad practice. Related: [https://stackoverflow.com/questions/70856563/vscode-g-it-isnt-finding-cpp-definition-files/70856902#70856902](https://stackoverflow.com/questions/70856563/vscode-g-it-isnt-finding-cpp-definition-files/70856902#70856902) – drescherjm Jul 03 '22 at 15:24
  • @drescherjm the `tasks.json` is this file exclusive only on VSCode? or does this file works on other Text Editor(like Sublime text)? – Evan Jul 04 '22 at 23:14
  • 1
    `tasks.json` is the default VSCode mechanism for building code. I don't know about Sublime text. At work (25 years on the same job) I nearly always use Visual Studio Community ( and the previous versions of the Visual Studio Product line). At home I test VSCode mainly to help answer questions here. – drescherjm Jul 05 '22 at 01:21

1 Answers1

1

Change main.cpp's includes to:

#include <iostream>
#include <string>
#include "MyClass.cpp"

By including MyClass.cpp in main.cpp and compiling like this:

g++ -o main main.cpp

you end up including MyClass.h by virtue of it being included in MyClass.cpp. By including MyClass.cpp, you can successfully compile.

If you want to keep your code as it currently is, you can instead change your compilation to be:

g++ -o main main.cpp MyClass.cpp

Which will include MyClass.cpp in compilation.

  • No need to change the include order (but I would do it the way you indicate - more readable convention). Issue is failure to include `MyClass.cpp` as part of the build. Would also discuss enabling warnings (e.g. `-Wall -Wextra -pedantic` and perhaps `-Werror` to treat warnings as errors) I think you captured the root cause. You could also `g++ -c MyClass.cpp` and then `g++ -o main main.cpp MyClass.o` and do it in 2-steps - like the Makefile would. – David C. Rankin Jul 03 '22 at 00:51
  • Thanks, changing to `#include "MyClass.cpp"` works for me – Evan Jul 03 '22 at 00:58
  • it was confusing for me because the tutorial I'm following are using `MyClass.h` the header – Evan Jul 03 '22 at 01:02