Vscode g++ 未找到 .cpp 定义文件

Vscode g++ it isn't finding .cpp definition files

我正在尝试使用多个 .cpp 和 .hpp 文件编译一个 c++ 示例,但 g++ 找不到任何成员函数定义。


main.cpp:

#include <iostream>
#include "Person.hpp"

int main()
{
    std::cout << "HELL!\n";
    
    Person a{"Jiraya"};
    std::cout << a.getName() << "\n";
    a.setName("Niko");
    a.do_smt();
}

Person.hpp:

#pragma once

#include <string>

using std::string;

class Person
{
private:
    string name;

public:
    Person();
    Person(const string &n);
    void do_smt();
    string getName(){return name;}
    void setName(const string& n);

Person.cpp:

    #pragma once
    #include <iostream>
    #include "Person.hpp"
    
    Person::Person(const string &n) : name{n}
    {
    }
    
    void Person::setName(const string &n)
    {
        name = n;
    }
    
    void Person::do_smt()
    {
        std::cout << "???";
    }

Tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-iquote${workspaceFolder}/headers"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

终端输出:

> Executing task: C/C++: g++ build active file <

Starting build...
/usr/bin/g++ -fdiagnostics-color=always -g /home/raijin/Documents/Code/C++/sandbox/main.cpp -o /home/raijin/Documents/Code/C++/sandbox/main -iquote/home/raijin/Documents/Code/C++/sandbox/headers
/usr/bin/ld: /tmp/ccN0pJKE.o: in function `main':
/home/raijin/Documents/Code/C++/sandbox/main.cpp:9: undefined reference to `Person::Person(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /home/raijin/Documents/Code/C++/sandbox/main.cpp:11: undefined reference to `Person::setName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /home/raijin/Documents/Code/C++/sandbox/main.cpp:12: undefined reference to `Person::do_smt()'
collect2: error: ld returned 1 exit status

Build finished with error(s).

Terminal will be reused by tasks, press any key to close it.

项目文件夹结构如下:

我确实在 tasks.json 中添加了 "-iquote${workspaceFolder}/headers" arg 以在子目录中找到 .hpp。它似乎不适用于 .cpp 文件。我该怎么办?(即使我将 Person.cpp 移动到 ${workspaceFolder} 结果相同的终端输出)

在您的 tasks.json 中,您使用的是默认值 ${file},这意味着仅编译活动文件,而不是文件夹结构中的所有源文件。

VSCode 文档解释了如何解决所有源文件都在同一文件夹中的情况:https://code.visualstudio.com/docs/cpp/config-linux#_modifying-tasksjson

修复方法是将 ${file} 替换为 "${workspaceFolder}/*.cpp"

在您的情况下,您有不止 1 个包含源文件的文件夹。您可以通过添加以下内容对第二个文件夹应用类似的修复:"${workspaceFolder}/classes/*.cpp"

所以整个 tasks.json 将是:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${workspaceFolder}/*.cpp",
                "${workspaceFolder}/classes/*.cpp",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-iquote${workspaceFolder}/headers"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}