1

I am new to vs code, after previously using atom a little bit. I am working on c++ with a program that has some classes in separated files. I have already installed the c/c++ extension, but I am facing a problem. When I try to run the program with all it's files included, it shows me the following error:

main.cpp:(.text+0x1f): undefined reference to `Car::sayHello()' collect2: error: ld returned 1 exit status. the code is pretty basic.

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

 using namespace std;
 int main(){
     Car c;
     c.sayHello();
      cout << "Hello world";
      return 0;
  }

From what I remember from atom, selecting all the files and running them would solve the problem, which I think is the same as running the following command in the terminal:

 g++ Car.cpp Car.h main.cpp -o Classes

I have tried these in vs code and the problem seems to appear again. Help is much appreciated.

dprozz122
  • 79
  • 8

3 Answers3

1

So if somebody else might have the same problem as me in the future. The problem was that I had declared the constructor and the destructor in the header file, but I hadn't defined them in the .cpp file, after doing that the program seems to be running well. Also as Fred mentioned Car.h is redundant to be mentioned in compilation process, but anyways the program will compile successfully, even if it is used.

dprozz122
  • 79
  • 8
0

So I had the same problem but I figured it out thanks to this link. Follow the instructions and test it out and it should work. There are also other ways to solve this problem based on your preference that are shown on this website. I did notice, however, that once you add multiple folders to your workspace, compiling whatever main.exe file you want becomes difficult since you use the arg ${workspaceFolder}\\*.cpp to compile all the .cpp files in your workspace folder. One way to solve that is to create multiple workspaces, but if anyone knows a more efficient way to do this, please let me know.

0

you can install fleeox in VS code that will help