Ubuntu上VScode编译C++

VScode编译C++主要有如下几种种方法。

方法一、配置  launch.json 和 tasks.json 文件。

在安装完vscode,下载相应的插件后,编写C++程序并进行编译。在编译之前首先需要配置vscode的两个文件,

分别为: launch.json 和 tasks.json 。

1、下载编辑C++ 的相应插件: C/C++,如下图第一个插件

   

建立文件夹 test ,并在vscode中选择打开此文件夹 (vscode必须打开 .cpp文件所在的文件夹才能进编译,而不能只是打开一个 .cpp的单文件进行编译。)。在vscode中点击1处建立C01文件夹;在C01目录下,再点击2处,建立 .cpp 文件。目录结构如下图:

       

2、配置  launch.json 文件

 (1)、打开01.cpp文件,按F5,在出现的命令提示栏上选择 C++(GDB/LLDB),

 (2)、 然后在出现的launch.json模板上进行修改,主要修改2个地方。分别为下图的3处,为运行编译程序的路径;4处为添加的在启动运行程序之前,要进行先编译,与下一个文件 tasks.json 相对应。launch.json文件用于运行编译的程序。

  1. 附 launch.json文件配置结果如下:
  2. {
  3. // Use IntelliSense to learn about possible attributes.
  4. // Hover to view descriptions of existing attributes.
  5. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  6. "version": "0.2.0",
  7. "configurations": [
  8. {
  9. "name": "(gdb) Launch",
  10. "type": "cppdbg",
  11. "request": "launch",
  12. "program": "${fileDirname}/${fileBasenameNoExtension}.out",
  13. "args": [],
  14. "stopAtEntry": false,
  15. "cwd": "${workspaceFolder}",
  16. "environment": [],
  17. "externalConsole": true,
  18. "MIMode": "gdb",
  19. "preLaunchTask": "build",
  20. "setupCommands": [
  21. {
  22. "description": "Enable pretty-printing for gdb",
  23. "text": "-enable-pretty-printing",
  24. "ignoreFailures": true
  25. }
  26. ]
  27. }
  28. ]
  29. }

3、配置 tasks.json 文件:

   (1)、按 ctrl+shift+P 或者 F1,来启动命令提示栏。在其中输入 run task,选择 Run Task,

    

 (2)、再点选 No task to run found,Configure Tasks... 

    

 (3)、再点选 Create tasks.json file from template

    

      (4)、点选 Others

      

       (5)、对出现的 tasks.json 进行修改。label 与上面的launch.json 中的 preLaunchTask 相对应。command 相当于在bash中编译 .cpp文件时的命令。

    

  (6)、最终修改的结果如下:

    

  1. 附 tasks.json 文件最终配置如下:
  2. {
  3. // See https://go.microsoft.com/fwlink/?LinkId=733558
  4. // for the documentation about the tasks.json format
  5. "version": "2.0.0",
  6. "tasks": [
  7. {
  8. "label": "build",
  9. "type": "shell",
  10. "command": "g++",
  11. "args": [
  12. "-g",
  13. "${file}",
  14. "-o",
  15. "${fileDirname}/${fileBasenameNoExtension}.out"
  16. ],
  17. "group": {
  18. "kind": "build",
  19. "isDefault": true
  20. }
  21. }
  22. ],
  23. "presentation": {
  24. "echo": true,
  25. "reveal": "always",
  26. "focus": false,
  27. //"panel": "shared",
  28. //"showReuseMessage": true,
  29. //"clear": false
  30. }
  31. }

4、回到 01.cpp 文件,按F5即可编译运行该程序,结果如下所示:

方法二、安装插件 Code Runner 来进行编译,如下图第一个插件。

  

点击箭头出的小三角即可编译

方法三、安装 C/C++ Compile Run 插件,按F6 可直接进行编译运行 .cpp 文件

注意:linux 系统上必须要安装有gcc/g++;windows系统上必须要安装有mingw 才可以。

二、总结

1、方法一可以进行断点调试,方法二、三只能进行编译运行。

2、vscode的Variables Reference

  1. Predefined variables :
  2. ${workspaceFolder} - the path of the folder opened in VS Code
  3. ${workspaceFolderBasename} - the name of the folder opened in VS Code
  4. without any slashes (/)
  5. ${file} - the current opened file
  6. ${relativeFile} - the current opened file relative to workspaceFolder
  7. ${fileBasename} - the current opened file 's basename
  8. ${fileBasenameNoExtension} - the current opened file's basename with no file extension
  9. ${fileDirname} - the current opened file 's dirname
  10. ${fileExtname} - the current opened file's extension
  11. ${cwd} - the task runner 's current working directory on startup
  12. ${lineNumber} - the current selected line number in the active file
  13. ${selectedText} - the current selected text in the active file
  14. ${execPath} - the path to the running VS Code executable
  15. Predefined variables examples
  16. Supposing that you have the following requirements:
  17. A file located at /home/your-username/your-project/folder/file.ext opened in your editor;
  18. The directory /home/your-username/your-project opened as your root workspace.
  19. So you will have the following values for each variable:
  20. ${workspaceFolder} - /home/your-username/your-project
  21. ${workspaceFolderBasename} - your-project
  22. ${file} - /home/your-username/your-project/folder/file.ext
  23. ${relativeFile} - folder/file.ext
  24. ${fileBasename} - file.ext
  25. ${fileBasenameNoExtension} - file
  26. ${fileDirname} - /home/your-username/your-project/folder
  27. ${fileExtname} - .ext
  28. ${lineNumber} - line number of the cursor
  29. ${selectedText} - text selected in your code editor
  30. ${execPath} - location of Code.exe

 

参考资料:

[1]  构建Visual Studio Code编译调试Linux C++环境

[2]  在Ubuntu中用Visual Studio Code编译调试C\C++

[3]  Visual Studio Code 如何编写运行 C、C++ 程序

[4]  VS Code 搭建 C/C++ 编译运行环境的三种方案