参考链接:https://www.cnblogs.com/chencarl/p/10463392.html 作者:chencarl

准备

安装vscode,可直接下载deb包进行安装,完成后安装C/C++ for Visual Studio Code插件,安装后重启(最新1.3版本以后不需要重启)。
1.linux下vscode的c  运行环境配置 - 图1

生成目录和文件

新建文件夹【test】,并新建文件helloworld.cpp文件,文件中内容如下,

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main(int argc, char const *argv[])
  5. {
  6. cout<< "hello world" << endl;
  7. return 0;
  8. }

使用vscode打开文件夹
1.linux下vscode的c  运行环境配置 - 图2

配置c++ IntelliSense

使用F1,打开命令选项,输入C/C++,选择C/C++:Edit configuration,生成c_cpp_properties.json配置文件。
1.linux下vscode的c  运行环境配置 - 图3

  1. {
  2. "configurations": [
  3. {
  4. "name": "Linux",
  5. "includePath": [
  6. "${workspaceFolder}/**"
  7. ],
  8. "defines": [],
  9. "compilerPath": "/usr/bin/gcc",
  10. "cStandard": "c11",
  11. "cppStandard": "c++17",
  12. "intelliSenseMode": "clang-x64"
  13. }
  14. ],
  15. "version": 4
  16. }

其中最主要为”includePath”的引用和库的路径,根据引用内容进行配置。

launch

在debug界面中选择添加配置,然后选择才c++(gdb/lgdb)选项,生成launch.json 顾名思义此文件主要服务于调试时的加载控制
1.linux下vscode的c  运行环境配置 - 图4

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "(gdb) Launch",
  6. "type": "cppdbg",
  7. "request": "launch",
  8. "program": "${workspaceFolder}/helloworld",
  9. "args": [],
  10. "stopAtEntry": false,
  11. "cwd": "${workspaceFolder}",
  12. "environment": [],
  13. "externalConsole": true,
  14. "MIMode": "gdb",
  15. "preLaunchTask": "build",
  16. "setupCommands": [
  17. {
  18. "description": "Enable pretty-printing for gdb",
  19. "text": "-enable-pretty-printing",
  20. "ignoreFailures": true
  21. }
  22. ]
  23. }
  24. ]
  25. }

需要注意的参数为”program”,此为需要调试的目标文件,应当设置为编译输出的文件位置;其次需要添加”preLaunchTask”,此项的名字应与下面所建的tasks.json中的任务名称一致。

tasks.json

在命令窗口中输入task,选择task: configure task选项生成tasks.json文件
1.linux下vscode的c  运行环境配置 - 图5

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "label": "build",
  6. "type": "shell",
  7. "command": "g++",
  8. "args":[
  9. "-g","helloworld.cpp","-o","helloworld"
  10. ],
  11. "group": {
  12. "kind": "build",
  13. "isDefault": true
  14. }
  15. }
  16. ]
  17. }

注意launch.json中的”preLaunchTask”调用与“label”相同的task。

开始调试

按下F5开始调试吧,一切就是这么简单,开始美好的旅程。
1.linux下vscode的c  运行环境配置 - 图6