c++ - 我如何配置 VS Code 才不会收到“未定义的引用”错误消息?
问题描述
我的工作环境:
EDI:Visual Studio 代码
C++ 编译器:GCC
扩展:
微软 C/C++
.run 代码运行器
我的源代码:
主文件
#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;
}
人物.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;
}
个人资料
#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__
错误消息:
Windows PowerShell 版权所有 (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 to
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): 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 topersonne::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 返回 1 退出状态PS T:\VSCC++\LEssentiel\chapitre 2 la programmation orientee objets\la_zim>
解决方案
您需要编辑由VScode生成的 task.json 。当您运行一个带有 main 函数的简单 .cpp 文件的调试时,tasks.json 的默认配置就足够了,但是如果您尝试使用带有类的个人头文件,您需要向 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"
}
在部分args中,我刚刚添加了我的额外文件 .cpp ( ej."${fileDirname}/extrefile.cpp" ) 的完整路径。
保存 tasks.json 并再次使用 F5 从具有 main 功能的 cpp 文件运行调试。
推荐阅读
- python - pyspark 从数据帧迭代 N 行到每次执行
- c++ - macOS 程序在通过自生成证书签名时崩溃,但未签名很好
- javascript - 通过 isomorphic-unfetch 获取时 nextjs api 不起作用
- php - 如何计算 JSON 对象中存在的元素数量?
- git - Git Stat:没有这样的文件或目录
- odata - 无法将更新的 oData 绑定到 sapui5 视图
- javascript - 如何在设置状态元素之前停止将其传递给子组件?
- java - 如何创建自定义 Jackson 注释以在序列化时附加属性
- asp.net-core - 使用 $select 或 $filter 时 OData 默认为帕斯卡大小写
- python - 来自端点的 AWS 负载均衡器