【问题标题】:running C++ code on VS code on Ubuntu and get this error collect2: error: ld returned 1 exit status在 Ubuntu 上的 VS 代码上运行 C++ 代码并收到此错误 collect2: error: ld returned 1 exit status
【发布时间】:2021-07-13 04:44:15
【问题描述】:

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

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

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

我收到了这个错误:未定义的对 `main' 的引用 collect2:错误:ld 返回 1 个退出状态

【问题讨论】:

  • 您的项目是如何设置的?
  • 代码很好。一定是配置错误。不幸的是,VSC 很容易做到。
  • @FantasticMrFox 我刚刚下载并添加了 C/C++ Microsoft 扩展和 Code Runner 扩展,就是这样。
  • @user4581301 我无法解决配置中的错误

标签: c++ ubuntu visual-studio-code


【解决方案1】:

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

`

{"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
            }
        }
    },
    
]}

`

【讨论】:

    猜你喜欢
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 2023-02-13
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多