首页 > 解决方案 > 在 Ubuntu 上的 VS 代码上运行 C++ 代码并收到此错误 collect2: error: ld returned 1 exit status

问题描述

我是 VS 代码的新手,我正在尝试运行基本的 C++ 代码:

#include<iostream>
using namespace std;
int main(){

cout<<"Hello World";
return 0;
}

我收到此错误:未定义对“main”collect2 的引用:错误:ld 返回 1 退出状态

标签: c++ubuntuvisual-studio-code

解决方案


我添加了此任务配置,它工作正常:

`

{"version": "2.0.0",
"tasks": [
    {
        "label": "debug",
        "type": "shell",
        "command": "",
        "args": ["g++","-g", "${relativeFile}", "-o","a.exe"]
    },
    {
        "label": "Compile and run",
        "type": "shell",
        "command": "",
        "args": [
            "g++","-g", "${relativeFile}", "-o","${fileBasenameNoExtension}.out", "&&", "clear" , "&&" , "./${fileBasenameNoExtension}.out"
        ],
        "group": {
            "kind": "build",
            "isDefault": true  
        },
        "problemMatcher": {
            "owner": "cpp",
            "fileLocation": ["relative", "${workspaceRoot}"],
            "pattern": {
                "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                "file": 1,
                "line": 2,
                "column": 3,
                "severity": 4,
                "message": 5
            }
        }
    },
    
]}

`


推荐阅读