如何使用Clang/GCC在Mac上为C/C++设置VSCode
How do I set up VSCode for C/C++ on Mac with Clang/GCC?
我一直在尝试为我的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
我强烈推荐你:
-
设置VSCode扩展环境;
-
配置C/C++编译和调试VSCode文件。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 在C++/Linux中设置单调时钟的一些技巧
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 嵌套在类中时无法设置成员数据
- 需要帮助设置在C++中使用的Potrace
- 如何在自删除后将对象设置为nullptr
- 将指针设置为"nullptr"并不能防止双重删除?
- 如何在Ubuntu中使用cmake设置qt4
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 如何在boost beast http请求中设置http头
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 如何在24位SDL_Surface上设置像素的颜色
- std::设置自定义比较器
- 如何设置一个范围来提取我想要获得的信息
- 如何在C/C++中用FD_set Unix设置套接字文件描述符
- 通过选项卡的文本设置QTabWidget顺序
- 将特征矩阵的向量设置为0
- 我可以检测和更改 gcc/g++ 中结构的当前数据对齐设置吗?