question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How i have to configure VS Code to don't get a “undefined reference” error message?

See original GitHub issue

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 © 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 to personne::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<char, std::char_traits<char>, std::allocator<char> >, 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 to personne::get_nomabi:cxx11’ collect2.exe: error: ld returned 1 exit status PS T:\VSCC++\LEssentiel\chapitre 2 la programmation orientee objets\la_zim>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

25reactions
babu-thomascommented, Oct 6, 2018

Hello @Pierre8r ,

The errors are due to personne.cpp not being compiled and linked with main.cpp. As far as I can tell, your current run command only compiles main.cpp. main.cpp includes the personne.h file but during linking g++ can’t find the definitions of the class functions because they are in personne.cpp.

To fix this change your run command for cpp in code-runner.executorMap to:

"code-runner.executorMap": {
        "cpp": "cd $dir && g++ -o $fileNameWithoutExt *.cpp && $dir$fileNameWithoutExt"
}

This command will compile and link all your .cpp files in the directory, and then run the compiled program.

If the above command doesn’t work, then also attach your settings.json file in your comment so that I can better figure out what’s going wrong.

1reaction
formulahendrycommented, Nov 1, 2018

Thanks @babu-thomas ! Closing this now. @Pierre8r , Feel free to reopen or create new issue if you have question.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c - undefined reference error in VScode - Stack Overflow
I have been able to reproduce the exact same error message using the ... undefined reference to `function' collect2: error: ld returned 1 ......
Read more >
How i have to configure VS Code to don't get a “undefined ...
Common error messages are error LNK2001 , error LNK1120 , error LNK2019 for Microsoft Visual Studio and undefined reference to symbolName for GCC....
Read more >
C++ Errors: Undefined Reference, Unresolved External ...
An “Undefined Reference” error occurs when we have a reference to object name (class, function, variable, etc.) in our program and the linker ......
Read more >
How to solve this problem in Visual Studio code, undefined ...
Hi Mihn! How do I solve this problem in Visual Studio code, undefined reference to “__imp_GetOpenFileNameA”? That is a linking error.
Read more >
[Solved] undefined reference to 'winmain@16' visual studio ...
Some times we run c++ program in VS code but the program doesn't compile and shows undefined reference to 'winmain@16' problem so here...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found