gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:05:42 26 4
gpt4 key购买 nike

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

我尝试按照微软的文档进行操作,但它设置了 .JSON 文件来编译和调试名为 helloworld.c 的程序,我只想配置 launch.json 和 task.json 来构建和调试任何 .c/.cpp 文件我给它。我对 .JSON 文件没有足够的经验,不知道我在做什么或做任何有用的事情。

任务.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
}
}
]

启动.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,您只需要 1 个部分,如果项目中有多个 cpp 文件,则需要添加“${fileDirname}/*.cpp”。这是我的 tasks.json 的样子:

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

此外,我在 vector 方面遇到了问题;来自 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
}

关于c++ - 如何使用 Clang/GCC 在 Mac 上为 C/C++ 设置 VSCode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60289021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com