如何设置VS Code以在OSX中构建,运行和调试简单的C ++文件?

时间:2018-11-09 18:21:52

标签: c++ macos visual-studio-code vscode-settings

我找到的最接近的类似答案是here

但这并不包含我的问题的答案。我还通过了docs,试图为VSCode提供C ++支持。我没有找到OSX特定的设置,而且我也不熟悉这种配置。有人可以帮忙吗 ?

更新:

为更清晰起见,添加task.json,launch.json,c_cpp_properties.json和终端输出的内容:

tasks.json:

{
"version": "2.0.0",
"tasks": [
  {
    "label": "build & debug file",
    "type": "shell",
    "command": "g++",
    "args": [
      "-g",
      "-o",
      "${fileBasenameNoExtension}",
      "${file}"
    ],
    "group": {
      "kind": "build",
      "isDefault": true
    }
  },
  {
    "label": "build & run file",
    "type": "shell",
    "command": "g++",
    "args": [
      "-o",
      "${fileBasenameNoExtension}",
      "${file}"
    ],
    "group": {
      "kind": "build",
      "isDefault": true
    },
    "problemMatcher": []
  }
]

}

launch.json:

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "(lldb) Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}/test",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "MIMode": "lldb"
    }
]

}

c_cpp_properties.json:

{
"configurations": [
    {
        "name": "Mac",
        "includePath": [
            "${workspaceFolder}/**",
            "/usr/local/Cellar/gcc/7.1.0/include/c++/7.1.0",
            "/usr/include/c++/4.2.1"
        ],
        "defines": [],
        "macFrameworkPath": [
            "/System/Library/Frameworks",
            "/Library/Frameworks"
        ],
        "compilerPath": "/usr/bin/clang",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "clang-x64"
    }
],
"version": 4 }

此配置面临的当前问题是,尝试时出现以下错误:Terminal-> Run Build Task-> build&debug file:

    ld: warning: ignoring file /Users/xyz/Workspace/VSCode/.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/xyz/Workspace/VSCode/.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)
The terminal process terminated with exit code: 1

1 个答案:

答案 0 :(得分:0)

调查来自ld的错误消息,我认为tasks.json会导致编译器/链接器选择错误的源文件,例如ld似乎正在处理您的/Users/xyz/Workspace/VSCode/.vscode/tasks.json

看一下task.json变量${file} here

的描述/示例

我认为在启动调试会话时,您在VSC中已打开tasks.json文件 focuseded 。因此,${file}被此打开的tasks.json文件替代。请把重点放在您的c / c ++ 源文件上,然后开始调试会话。

由于我使用Xcode附带的Apple命令行工具clang和lldb,因此我没有仔细检查您的配置文件的正确性。在我的情况下,VSC确实建议并为此环境生成了正确的默认配置文件。我正在使用Microsoft的C / C ++ 0.24.0扩展名。 (我只需要在"-std=c++11",中的args上添加tasks.json即可获得clang识别c++11的源代码。)