简体   繁体   中英

How to setup VS Code for Building, Running and Debugging simple C++ files in OSX?

The closest similar answer I found for it is here

But that doesn't contain an answer to my question. I also went through the docs for trying to have C++ support to VSCode. I didn't find OSX specific settings and I am not well versed with such configurations. Could someone please help ?

Update:

Adding the contents of tasks.json, launch.json, c_cpp_properties.json, and terminal output for more clarity:

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 }

Current problem I am facing with this configuration is that I get the following error when I try: 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

Looking into the error message from ld I think that the tasks.json causes the compiler/linker to pick up the wrong source file eg ld seems to process your /Users/xyz/Workspace/VSCode/.vscode/tasks.json .

Look at the description/example of tasks.json's variable ${file} here

I think you have the tasks.json file opened and focussed in VSC when starting the debug session. ${file} is therefore substituted with this opened tasks.json file. Please put the focus on your c/c++ source file , then start the debug session.

I did not closely examine the correctness of your configuration files since I use Apple's command line tools clang and lldb that come with Xcode. VSC did suggest and generated the correct default config files for this environment in my case. I'm use the C/C++ 0.24.0 extension from Microsoft. (I only had to add "-std=c++11", to the args in tasks.json to get clang recognise c++11 source code).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM