安装c/c++插件
打开插件页面,搜索输入c/c++搜索c/c++插件。
安装该插件后,使用vscode打开包含cpp文件的文件夹时,vscode会添加.vscode子文件夹到目录中。
添加c_cpp_properties.json配置
通过快捷键⇧⌘p运行c/cpp: edit configurations,添加缺失的c_cpp_properties.json文件。默认的添加的文件如下:
{ "configurations": [ { "name": "mac", "includepath": [ "${workspacefolder}/**" ], "defines": [], "macframeworkpath": [ "/applications/xcode.app/contents/developer/platforms/macosx.platform/developer/sdks/macosx10.14.sdk/system/library/frameworks" ], "compilerpath": "/usr/bin/clang", "cstandard": "c11", "cppstandard": "c++17", "intellisensemode": "clang-x64" } ], "version": 4}
这个部分没有做什么调整。采用的是默认添加的文件。
添加tasks.json配置文件
通过快捷键⇧⌘p选择执行的命令,选择task: configure task命令,选择create tasks.json from templates,选择others来创建一个外部命令。根据自己的编译器更换commnd选项。
{ // see https://go.microsoft.com/fwlink/?linkid=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "cpp", "type": "shell", "command": "g++", "args": [ "-g", "${file}", "-o", "a.out" ], "group": { "kind": "build", "isdefault": true } } ]}
这里需要说一点,希望对当前标签页的代码进行编译执行,因此args参数中用的是${file}。另外需要说的一点是,如果不指定输出的编译文件,会影响调试。
>> g++ -g question.cpp -o a.out
当然也可以把args中的a.out替换为${file}保持和文件名的对应。
添加launch.json配置文件
在调试界面点击运行,会提示添加launch.json配置文件。其定义了启动调试文件的相关属性。
{ // 使用 intellisense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(lldb) launch", "type": "cppdbg", "request": "launch", "program": "${workspacefolder}/a.out", "args": [], "stopatentry": false, "cwd": "${workspacefolder}", "environment": [], "externalconsole": false, "mimode": "lldb", "prelaunchtask": "cpp" } ]}
这里需要说明的有三个参数:
1、program参数,这里的文件名要和之前的保持对应,如果前面用的a.out,这里也应该是a.out。
2、externalconsole参数,悬停在上面会有说道,如果是linux等系统,将其置为false时,将会在vscode集成内输出打印内容。如果是true的话,会在外部的终端输出。为了在vscode看输出,这里设置其为false.另一个需要留意的是参数externalconsole,悬停在上面会有说道,如果是linux等系统,将其置为false时,将会在vscode集成内输出打印内容。如果是true的话,会在外部的终端输出。为了在vscode看输出,这里设置其为false。
3、prelaunchtask参数,因为每次调试都需要预先对代码进行编译,这里可以通过prelaunchtask参数指定编译任务。这里将其指定为之前的任务: cpp. 即之前task的label中的内容。
相关文章教程推荐:vscode教程
以上就是vscode怎么配置cpp调试环境的详细内容。