如何使用Clang/GCC在Mac上为C/C++设置VSCode

How do I set up VSCode for C/C++ on Mac with Clang/GCC?

本文关键字:C++ 设置 VSCode 上为 何使用 Clang GCC Mac      更新时间:2023-10-16

我一直在尝试为我的Mac安装Visual Studios Code,但我在launch.json和task.json方面遇到了问题。我将使用Clang编译器。

我试着遵循微软的文档,但它设置了.JSON文件来编译和调试一个名为helloworld.c的程序,我只想配置launch.JSON和task.JSON来构建和调试我给它的任何.c/.cpp文件。我对.JSON文件没有足够的经验,不知道我在做什么或做什么。

tasks.json:

{
"version": "2.0.0",
"tasks": [
{
"label": "clang++ build active file",
"type": "shell",
"command": "clang++",
"options": {
"cwd": "${workspaceRoot}"
},
"group": "build",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"args": [
"-std=c++17",
"-stdlib=libc++",
"--debug"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"absolute"
],
"pattern": {
"regexp": "^(.*):(\d+):(\d+):\s+(warning|error):\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
{
"type": "shell",
"label": "clang build active file",
"command": "/usr/bin/clang",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]

}

launch.json:

"version": "0.2.0",
"configurations": [
{
"name": "clang build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "clang build",
"miDebuggerPath": "/usr/bin/lldb"
}
]
}

我也在为此而挣扎。我现在已经开始工作了。对于tasks.json,你只需要一个部分,如果你在项目中有多个cpp文件,那么你需要添加"${fileDirname}/*.cpp">

{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${fileDirname}/*.cpp",
// "${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

此外,我对矢量有问题;vscode示例中的错误。如果遇到同样的问题,请尝试在.vscode目录中添加一个c_cpp_properties.json文件,如下所示:

{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}

启动:

{
"version": "0.2.0",
"configurations": [
{
// MacOS
"name": "Launch Program(lldb)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/no1_2",
"args": [
"4",
"3",
"2",
"1"
],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}

任务:

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Build with Clang",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"${file}",
"-o",
"1.out",
"-debug",
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

c_cpp:

{
"configurations": [
{
"name": "Mac",
"includePath": ["${workspaceFolder}/**"],
"defines": [],
"macFrameworkPath": [           "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}

希望帮助你

如果您只想在mac、上的VSCode中编译或调试任何one C/C++ file

我强烈推荐你:

  1. 设置VSCode扩展环境;

  2. 配置C/C++编译和调试VSCode文件。