c++ - 无法在新安装的 Visual Studio Code 上运行 C++ 代码

标签 c++ visual-studio-code

我做了以下尝试在 macOS 上为 C++ 设置开发环境,但没有成功。

  1. 我从 Microsoft 网站下载并正确安装了 Visual Studio Code。

  2. 我安装了 ms-vscode.cpptools 和 mitaki28.vscode-clang。

  3. 我安装了 gdb。

以下是我收到的警告:

ld: warning: ignoring file /Users/andy/Documents/VS Code C++/Hello World/.vscode/tasks.json, file was built for unsupported file format ( 0x7B 0x0A 0x20 0x20 0x20 0x20 0x22 0x76 0x65 0x72 0x73 0x69 0x6F 0x6E 0x22 0x3A ) which is not the architecture being linked (x86_64): /Users/andy/Documents/VS Code C++/Hello World/.vscode/tasks.json

Undefined symbols for architecture x86_64:  
  "_main", referenced from:  
     implicit entry/start for main executable  
ld: symbol(s) not found for architecture x86_64  
clang: error: linker command failed with exit code 1 (use -v to see invocation)  

编辑 1:我的平台是 macOS。

编辑 2:我在尝试构建最简单的 Hello World 项目时出现了问题,因此问题不可能出在我的 C++ 代码上。但我几乎是一个新手,所以我不知道 .json 是什么东西。

编辑 3:我认为问题可能出在 launch.json 文件中,所以这是我的文件:

{
"version": "0.2.0",
"configurations": [
    {
        "name": "C++ Launch (GDB)",
        "type": "cppdbg",
        "request": "launch",
        "targetArchitecture": "x64",
        "program": "${file}.out",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceRoot}",
        "environment": [],
        "externalConsole": true,
        "preLaunchTask": "g++",
        "miDebuggerPath": "/usr/local/bin/gdb"
    }
]

最佳答案

看来您的项目中没有包含

{somefile}.cpp 文件(或者该文件未被编译)
int main() {
    ...
    return 0;
}

int main(int argc, char ** argv) {
    ...
    return 0;
}

函数。

您可以安全地忽略 tasks.json 警告 - 它被错误地传递给链接器输入,链接器只是忽略该文件。

关于c++ - 无法在新安装的 Visual Studio Code 上运行 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45709338/

相关文章:

c++ - 如何使用C++中的插槽图/对象池模式管理数百万个游戏对象?

c++ - 在 C++ 环境中使用 OpenCV 绘制边界框

javascript - 如何在 VScode 中设置 typescript ?

ubuntu - 如何在 VSCode 中更改空间的大小?

typescript - 如何使用 DebugSession.customRequest 运行任意 gdb 命令并获取结果

c++ - sys/types.h 和 linux/types.h 的冲突重新声明

c++ - 实现通信超时

javascript - VSCode 如果在省略大括号时缩进

node.js - 为什么更新VSCode后给出此错误?无法读取…(Node.js)的源映射

c++ - 如何使用 Direct3D9 缩放 IDirect3DTexture9?