首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VSCode仅构建main.cpp文件,而不是文件夹中的所有文件

VSCode仅构建main.cpp文件,而不是文件夹中的所有文件
EN

Stack Overflow用户
提问于 2021-11-03 21:26:46
回答 1查看 71关注 0票数 1

对不起,我是一个初学者,我正在尝试运行以下视频中的代码:https://www.youtube.com/watch?v=gq2Igdc-OSI&ab_channel=thenewboston

下面是我的代码:

main.cpp

代码语言:javascript
复制
    #include <iostream>
    #include "Mother.h"
    #include "Daughter.h"
    using namespace std;

int main() {
    Mother mom;
    mom.sayName();
}

Mother.h

代码语言:javascript
复制
#ifndef MOTHER_H
#define MOTHER_H

class Mother 
{
    public:
        Mother();
        void sayName();
};

#endif

Mother.cpp

代码语言:javascript
复制
#include <iostream>
#include "Mother.h"
#include "Daughter.h"
using namespace std;

Mother::Mother()
{
}

void Mother::sayName() {
    cout << "I am a Roberts!" << endl;
}

Daughter.h

代码语言:javascript
复制
#ifndef DAUGHTER_H
#define DAUGHTER_H

class Daughter 
{
    public:
        Daughter();
};

#endif

Daughter.cpp

代码语言:javascript
复制
#include <iostream>
#include "Mother.h"
#include "Daughter.h"
using namespace std;

Daughter::Daughter()
{

}

我的错误是:“未定义的引用`母亲::sayName()‘收集器2.exe:错误: ld返回1退出状态”我做错了什么?和视频一模一样,我是不是漏掉了什么?它们都在同一个文件夹中。我使用的是VS Code。

这是我的tasks.json文件,我应该修改什么?

代码语言:javascript
复制
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Users\\Taro\\Desktop\\MinGW\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${workspaceFolder}\\*.cpp",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: C:\\Users\\Taro\\Desktop\\MinGW\\bin\\g++.exe"
        }
    ]
}

以下是终端中的错误消息:

代码语言:javascript
复制
PS C:\Users\Taro\Desktop\CIS 250> cd "c:\Users\Taro\Desktop\CIS 250\" ; if ($?) { g++ main.cpp -o main } ; if ($?) { .\main }
c:/users/taro/desktop/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Taro\AppData\Local\Temp\ccBwABJt.o:main.cpp:(.text+0x15): undefined reference to `Mother::Mother()'
c:/users/taro/desktop/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Taro\AppData\Local\Temp\ccBwABJt.o:main.cpp:(.text+0x21): undefined reference to `Mother::sayName()'
collect2.exe: error: ld returned 1 exit status
EN

回答 1

Stack Overflow用户

发布于 2021-11-03 22:27:01

通常,使用VSCommunity构建cpp可能更容易,因为这也是免费的(尽管可能比您需要的更重)。尝试从命令行构建程序,而不是通过VSCode来查看是否发生了链接。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69831934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档