C++ 如何将VS代码配置为don';“我得不到一个”;“未定义引用”;错误消息?
我的工作环境: EDI:VisualStudio代码 C++编译器:GCC 扩展: 微软C/C++ .运行代码运行程序 我的源代码: main.cppC++ 如何将VS代码配置为don';“我得不到一个”;“未定义引用”;错误消息?,c++,visual-studio-code,C++,Visual Studio Code,我的工作环境: EDI:VisualStudio代码 C++编译器:GCC 扩展: 微软C/C++ .运行代码运行程序 我的源代码: main.cpp #include <iostream> #include "personne.h" int main() { personne jojo("fabien"); std::cout <<"la personne s'appelle "<<jojo.get_nom()<<" et a "
#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;
}
人事部
#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__
\ifndef\uu人员__
#定义人员__
#包括
班主任{
std::字符串名称;
int age;在此处输入代码
公众:
std::字符串get_nom();
int get_age();
人员(标准:字符串);
人员(标准:字符串,整数);
};
#endif/\u人员__
错误消息:
Windows PowerShell版权所有(C)微软公司。我们享有服务权
PS T:\VSCC++\Reducetiel>cd“T:\VSCC++\Reducetiel\chapitre 2 la
面向对象的程序设计;if($?){g++main.cpp-o
主};如果($?){.\main}
C:\Users\Pierre\AppData\Local\Temp\ccKhfKRw.o:main.cpp:(.text+0x4e):
未定义的引用
personne::personne(标准:uuCXX11::基本字符串)'
C:\Users\Pierre\AppData\Local\Temp\ccKhfKRw.o:main.cpp:(.text+0x72):
对
personne::get_age()的未定义引用
C:\Users\Pierre\AppData\Local\Temp\ccKhfKRw.o:main.cpp:(.text+0x87):
对personne::get_nom[abi:cxx11]()的未定义引用
C:\Users\Pierre\AppData\Local\Temp\ccKhfKRw.o:main.cpp:(.text+0x137):
未定义的引用
personne::personne(std::\uCXX11::basic\uString,std::allocator>,int)'
C:\Users\Pierre\AppData\Local\Temp\ccKhfKRw.o:main.cpp:(.text+0x15b):
对personne::get_age()的未定义引用
C:\Users\Pierre\AppData\Local\Temp\ccKhfKRw.o:main.cpp:(.text+0x170):
未定义对
personne::get_nomabi:cxx11'collect2.exe的引用:
错误:ld返回了1个退出状态PS T:\VSCC++\LESSETIEL\chapitre 2
la programmation orientee objets\la_zim>
您需要使用personne.cpp编译和编辑k。将其添加到项目中,以便命令行提及所有源文件 我也有同样的问题,似乎是链接器错误。 您应该使用嵌入在vsc中的终端,并使用字符串构建文件
g++-g-o main.cpp personne.cpp
然后简单地执行名为main的可执行文件。
您还应该尝试将文件personne.cpp添加到tasks.json文件中您需要编辑VScode生成的task.json。 当您使用main函数运行简单的.cpp文件调试时,tasks.json的默认配置就足够了,但是如果您尝试使用带有类的personal headers files,则需要向tasks.json指示您的程序需要编译哪些额外的文件 像这样:
{
"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"
}
{
“任务”:[
{
“类型”:“外壳”,
“标签”:“g++生成活动文件”,
“命令”:“/usr/bin/g++”,
“args”:[
“-g”,
“${file}”,“${fileDirname}/Node.cpp”,1)请添加有关如何编译源代码的信息。2)相关:。它看起来不像是正在编译personne.cpp
。
{
"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"
}