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:\UsersPierreAppDataLocal\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:\UsersPierreAppDataLocal\Temp\ccKhfKRw.o:main.cpp:(.text+0x87):
undefined reference topersonne::get_nom[abi:cxx11]()' C:\Users\Pierre\AppData\Local\Temp\ccKhfKRw.o:main.cpp:(.text+0x137): undefined reference topersonne::personne(std::__cxx11::basic_stringstd::char_traits , std::allocator >, int)'
C:\UsersPierreAppDataLocal\Temp\ccKhfKRw.o:main.cpp:(.text+0x15b):
undefined reference topersonne::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>
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.
Hello @babu-thomas ,
I think you're right.
But I do not have time to apply for the moment your advice.
As soon as I have time I apply your advice, and you give me some news.
Thank you.
Thanks @babu-thomas ! Closing this now. @Pierre8r , Feel free to reopen or create new issue if you have question.
Thanks, @babu-thomas .I just came into this issue and with your method , the problem is well solved.
It does not work on my vscode environment, get this error.


Some wierd things happaned, when I tryed to inlude a .h file which I already defined, it will get errors, however, when I include the .cpp file which define members in the .h file, it works.
I guess the main.cpp does not link .o
"files.defaultLanguage": "c++", // ctrl+N新建文件后默认的语言
"editor.formatOnType": true, // (对于C/C++)输入分号后自动格式化当前这一行的代码
//"editor.suggest.snippetsPreventQuickSuggestions": false, // clangd的snippets有很多的跳转点,不用这个就必须手动触发Intellisense了
"editor.acceptSuggestionOnEnter": "off", // 我个人的习惯,按回车时一定是真正的换行,只有tab才会接受Intellisense
// "editor.snippetSuggestions": "top", // (可选)snippets显示在补全列表顶端,默认是inline
"code-runner.runInTerminal": true, // 设置成false会在“输出”中输出,无法输入
//"code-runner.executorMap": {
// "c": "cd $dir && clang '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc --target=x86_64-w64-mingw -std=c11 && &'$dir$fileNameWithoutExt'",
// "cpp": "cd $dir && clang++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc --target=x86_64-w64-mingw -std=c++17 && &'$dir$fileNameWithoutExt'"
// "c": "cd $dir && clang $fileName -o $fileNameWithoutExt.exe -Wall -g -O2 -static-libgcc --target=x86_64-w64-mingw -std=c11 && $dir$fileNameWithoutExt",
// "cpp": "cd $dir && clang++ $fileName -o $fileNameWithoutExt.exe -Wall -g -O2 -static-libgcc --target=x86_64-w64-mingw -std=c++17 && $dir$fileNameWithoutExt"
//}, // 控制Code Runner命令;未注释的仅适用于PowerShell(Win10默认),文件名中有空格也可以编译运行;注释掉的适用于cmd(win7默认),也适用于PS,文件名中有空格时无法运行
"code-runner.executorMap": {
"cpp": "cd $dir && g++ -o $fileNameWithoutExt *.cpp && $dir$fileNameWithoutExt"
},
"code-runner.saveFileBeforeRun": true, // run code前保存
"code-runner.preserveFocus": true, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false
"code-runner.clearPreviousOutput": false, // 每次run code前清空属于code runner的终端消息,默认false
"code-runner.ignoreSelection": true, // 默认为false,效果是鼠标选中一块代码后可以单独执行,但C是编译型语言,不适合这样用
"C_Cpp.clang_format_sortIncludes": true, // 格式化时调整include的顺序(按字母排序)
"C_Cpp.errorSquiggles": "Disabled", // 因为有clang的lint,所以关掉;如果你看的是别的答主用的不是vscode-clangd,就不要加这个了
"C_Cpp.autocomplete": "Disabled", // 同上;这几条也可以考虑放到全局里,否则很多错误会报两遍,cpptools一遍clangd一遍
"C_Cpp.suggestSnippets": false,
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4 }",
"files.associations": {
"vector": "cpp"
}, // 同上
"editor.fontSize": 17,
"cSpell.words": [
"h",
"myfile",
],
"C_Cpp.dimInactiveRegions": false
}
Thanks @babu-thomas you saved my day.
Most helpful comment
Hello @Pierre8r ,
The errors are due to
personne.cppnot being compiled and linked withmain.cpp. As far as I can tell, your current run command only compilesmain.cpp.main.cppincludes thepersonne.hfile but during linkingg++can't find the definitions of the class functions because they are inpersonne.cpp.To fix this change your run command for
cppincode-runner.executorMapto:This command will compile and link all your
.cppfiles in the directory, and then run the compiled program.If the above command doesn't work, then also attach your
settings.jsonfile in your comment so that I can better figure out what's going wrong.