3

My work environment :

EDI: Visual Studio Code

C ++ Compiler: GCC

Extensions:

Microsoft C / C ++

.run Code Runner

My source code :

main.cpp

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

int main() {

 personne jojo("fabien");

 std::cout <<"la personne s'appelle "<<jojo.get_nom()<<" et a " 
 <<jojo.get_age()<<" ans "<<std::endl;

 personne titi("lena",3);

 std::cout <<"la personne s'appelle "<<titi.get_nom()<<" et a " 
 <<titi.get_age()<<" ans "<<std::endl;
}

personne.cpp

#include "personne.h"

std::string personne::get_nom() {
    return nom;
}
int personne::get_age() {
    return age;
}

personne::personne(std::string n){
    nom=n;
    age=0;
}

personne::personne(std::string n, int a) {
    nom=n;
    age=a;
}

personne.h

#ifndef __PERSONNE__
#define __PERSONNE__

#include <string>

class personne {
    std::string nom;
    int age;enter code here

public :
    std::string get_nom();
    int get_age();

    personne(std::string);
    personne(std::string, int);
};

#endif // __PERSONNE__

Errors messages :

Windows PowerShell Copyright (C) Microsoft Corporation. Tous droits réservés.

PS T:\VSCC++\LEssentiel> cd "t:\VSCC++\LEssentiel\chapitre 2 la programmation orientee objets\la_zim\" ; if ($?) { g++ main.cpp -o main } ; if ($?) { .\main } C:\Users\Pierre\AppData\Local\Temp\ccKhfKRw.o:main.cpp:(.text+0x4e): undefined reference to personne::personne(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' C:\Users\Pierre\AppData\Local\Temp\ccKhfKRw.o:main.cpp:(.text+0x72): undefined reference topersonne::get_age()' C:\Users\Pierre\AppData\Local\Temp\ccKhfKRw.o:main.cpp:(.text+0x87): undefined reference to personne::get_nom[abi:cxx11]()' C:\Users\Pierre\AppData\Local\Temp\ccKhfKRw.o:main.cpp:(.text+0x137): undefined reference to personne::personne(std::__cxx11::basic_string, std::allocator >, int)' C:\Users\Pierre\AppData\Local\Temp\ccKhfKRw.o:main.cpp:(.text+0x15b): undefined reference to personne::get_age()' C:\Users\Pierre\AppData\Local\Temp\ccKhfKRw.o:main.cpp:(.text+0x170): undefined reference topersonne::get_nomabi:cxx11' collect2.exe: error: ld returned 1 exit status PS T:\VSCC++\LEssentiel\chapitre 2 la programmation orientee objets\la_zim>

Pierre8r
  • 33
  • 1
  • 1
  • 3
  • 1) Please add the information on how you are compiling the sources. 2) Related: [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). – Algirdas Preidžius Oct 05 '18 at 13:40
  • It does not look like `personne.cpp` is being compiled. – drescherjm Oct 05 '18 at 13:49

3 Answers3

4

you need to edit the task.json, generated by VScode. When you run a debuging of a simple .cpp file with a main function, the default configuration of tasks.json that´s enough, but if you try to use a personal headers files with classes, you need to indicate to tasks.json which extra files you program need to compile.

Like this:

  {
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}","${fileDirname}/Node.cpp", <============= HERE <======
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            }
        }
    ],
    "version": "2.0.0"
}

In the secction args, I just added the full path of my extra file .cpp (ej."${fileDirname}/extrefile.cpp").

Save the tasks.json and run again the Debugging with F5, from the cpp file with main function.

codemirror
  • 3,164
  • 29
  • 42
0

You need to compile and li k with personne.cpp. Add it to your project so that the command line mentions all your source files.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
0

I have the same problem and it seems a linker error. You should use the terminal embedded in vsc and build your file using the string g++ -g -o main main.cpp personne.cpp
Then simply execute the executable file called main. You should also try to add the file personne.cpp to tasks.json file

Biga
  • 37
  • 6