【问题标题】:VSCode - C++ undefined reference to 'CLASS::FUNCTION'VSCode - C++ 未定义的对 'CLASS::FUNCTION' 的引用
【发布时间】:2020-05-27 20:32:51
【问题描述】:

我正在使用 VSCode 创建一个 C++ 项目,并在尝试构建 && 调试时不断收到此错误。从控制台运行时,如果我使用 'g++ -o main 'main.cpp' 'file_1.cpp' 'file_2.cpp'' 它可以正常工作并正确编译。

我读到这与文件链接有关?有谁知道如何在 VSCode 中解决这个问题?如果有帮助,我有一个默认的 launch.json 配置文件,它会构建活动文件。

这是我的 tasks.json 文件的内容:

{
"version": "2.0.0",
"tasks": [
    {
        "type": "shell",
        "label": "shell: g++ build active file",
        "command": "/usr/bin/g++",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "/usr/bin"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    },
    {
        "type": "shell",
        "label": "g++ build active file",
        "command": "/usr/bin/g++",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "/usr/bin"
        }
    }
]

}

编辑:我正在使用 Debian 10 'Buster'

非常感谢

【问题讨论】:

  • 我查看了 c/c++ 配置,有一个添加编译器参数的选项,但我似乎找不到任何指定文件的方法(理想情况下,它需要递归搜索工作区所有 .cpp 文件)
  • 这应该在 .json 文件中。
  • @drescherjm 我正在使用 g++ 在 Linux 上编译
  • 您可以在其中列出文件,就像您在命令行中所做的那样。您可能还想考虑使用 CMake 之类的东西,而不必一直在 tasks.json 文件中进行更改

标签: c++ linux visual-studio-code vscode-settings vscode-tasks


【解决方案1】:

您需要修改 tasks.json 以编译所有 .cpp 。 在args 中将${file} 更改为${workspaceFolder}/*.cpp 要运行构建任务,请按 Ctrl+Shift+B,这会在当前目录中生成一个执行文件。在终端执行此文件以运行程序(./fileName)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    相关资源
    最近更新 更多