如何在OSX中设置用于构建、运行和调试简单C++文件的VS代码



我找到的最相似的答案是这里的

但这并不能回答我的问题。我还查阅了试图让C++支持VSCode的文档。我没有找到OSX特定的设置,我也不熟悉这样的配置。有人能帮忙吗?

更新:

添加tasks.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 }

我目前面临的这个配置的问题是,我在尝试时遇到以下错误:终端->运行构建任务->构建&调试文件:

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

查看来自ld的错误消息,我认为tasks.json导致编译器/链接器获取错误的源文件,例如ld似乎处理了您的/Users/xyz/Workspace/VSCode/.vscode/tasks.json

看看tasks.json的变量${file}的描述/示例

我认为在启动调试会话时,您已经在VSC中打开了tasks.json文件并集中了。因此CCD_ 7被这个打开的CCD_。请将重点放在c/c++源文件上,然后启动调试会话。

我没有仔细检查您的配置文件的正确性,因为我使用了Xcode附带的苹果命令行工具clang和lldb。在我的案例中,VSC确实为这个环境建议并生成了正确的默认配置文件。我使用的是微软的C/C++0.24.0扩展。(我只需要将"-std=c++11",添加到tasks.json中的args中,就可以让clang识别c++11源代码(。

最新更新