VS Code 在Linux下IDE开发C++的HelloWorld

用Visual Studio Code 在Linux(Ubuntu)下构造c++ 的集成开发环境,编辑,编译和调试运行一个简单程序HelloWorld。

想达到上面目标,搜索到以下文章,学习验证而成本文日记。

链接是:https://code.visualstudio.com/docs/cpp/config-linux

前期准备

运行环境是ubuntu16.0,先安装好Visual Studio Code(VS Code)

安装好VS Code 的C++ 扩展,可以在VS code里Ctrl+Shift+X,然后输入C++

确保gcc 安装好了,检查方法是:

gcc --version

如果没有,先更新系统

sudo apt-get update

然后如下安装gcc 和 gdb

sudo apt-get install build-essential gdb

我的ubuntu 16 这样后,gcc版本是5.8,有点低。

建立helloworld

在终端建立一个helloworld目录,然后启动VScode,如下:

mkdir helloworld
cd helloworld
code .

本实验完成后,将在工作空间目录建立3个文件

  • tasks.json (编译链接设置)
  • launch.json (调式设置)
  • c_cpp_properties.json (c++属性设置)

在文件资源管理器标题栏中,选择“新建文件”并命名文件helloworld.cpp

编辑或粘贴下面代码:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    // Create an empty vector 
    vector<int> vect;  
     
    vect.push_back(10); 
    vect.push_back(20); 
    vect.push_back(30);
	
    for (int i = 0; i <vect.size(); i++)
    {
        cout << vect[i] << endl;
    }
    cout << endl;
}

原文的代码要求gcc版本比较高,我修改如上,你也可以改成更简单的代码。

ctrl + S 保存文件

VScode 的文件浏览器里可以看到这个文件:

编译helloworld

这一步将建立tasks.json 文件。

主菜单里 Terminal > Configure Default Build Task

一个下拉菜单里让你选择编辑器

根据你的情况选择编译工具,这就是上文要准备好的gcc, 上图我是复制的,我自己的界面不一样,当然类似。

这样就建立了tasks.json:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "g++ build active file",
      "command": "/usr/bin/g++",
      "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
      "options": {
        "cwd": "/usr/bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

更多task.json 变量请参考 variables reference.

在这里${file}指当前活动文件,当打开文件helloworld.cpp,就是helloworld.cpp。

编译链接

回到helloworld.cpp, 这个很重要,这时 ${file} =helloworld.cpp ,

按Ctrl+Shift+B 或者主菜单>Terminal>Run Build Task

编译链接就按task.json设置编译链接。

有错就报错,没有报错,应该如下类似显示:

> Executing task: /usr/bin/g++ -g /home/liwenz/helloworld/helloworld.cpp -o /home/liwenz/helloworld/helloworld <


Terminal will be reused by tasks, press any key to close it.

我们可以看到参数的替换 

/home/liwenz/helloworld/helloworld.cpp =>  ${file}

/home/liwenz/helloworld/helloworld=>${fileDirname}/${fileBasenameNoExtension}

走完第一个helloworld流程后,再做这个helloworld时,我没注意,编译时打开tasks.json , 结果总报错,我当时还不会看错误,错误指示如下:

Executing task: /usr/bin/g++ -g /home/liwenz/helloworld/.vscode/tasks.json -o /home/liwenz/helloworld/.vscode/tasks <

/usr/bin/ld:/home/liwenz/helloworld/.vscode/tasks.json: file format not recognized; treating as linker script /usr/bin/ld:/home/liwenz/helloworld/.vscode/tasks.json:1: syntax error collect2: error: ld returned 1 exit status The terminal process terminated with exit code: 1

现在回过头来看,看到 task 的内容,就知道怎么错了。

修改tasks.json

上面的task.json 可以根据我们需要修改。

如果要编译多个.cpp 文件,可以修改

"${workspaceFolder}/*.cpp" 替换 ${file}

输出则固定名字,比如hello 替换"${fileDirname}/${fileBasenameNoExtension}"

这样就不必一定要回到helloworld.cpp 然后才能编译链接。

Debug helloworld.cpp

这一步我们创建 launch.json

在主菜单 Run > Add Configuration... 然后选择 C++ (GDB/LLDB)

在下拉菜单里选择 g++ build and debug active file.

这样就形成了一个launch.json 文件

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "g++ build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "g++ build active file",
      "miDebuggerPath": "/usr/bin/gdb"
    }
  ]
}

 缺省情况下, stopAtEntry 设置成 false, 这样不会有任何断点,修改为true, 则Debug时会停在 main 函数开始处。

开始调试

回到打开helloworld.cpp 的状态,F5 或者菜单 Run > Start Debugging 就开始调试了。同样没有修改这个launch.json ,必须回到helloworld.cpp 因为里面包含了${file}等。

可以预先设置断点,或者至少stopAtEntry修改为true。设置断点的方法可以鼠标在行前直接点直接改变,也可以光标到一行,然后F9 或者菜单 Run > Toggle Breakpoint,乒乓改变断点状态。还有Run>Add New Breakpoint方法,先不学。

停到断点后,出现下面Debug 控制条,意义鼠标放在上面有提示,分别是暂停(下图中为灰),继续运行,单步,单步内进,单步外退,重新开始,停止。

可以Add Watch ,也可以看到各变量的值

C/C++ configurations(配置)

如果要更进一步配置c/c++,则可以用 c_cpp_properties.json 文件。

Ctrl+Shift+P  然后输入c/c++ 

这里只是简单提一下。

配置的重用

把配置文件复制到新工程的 .vscode 目录下即可,然后根据情况修改这几个json 文件。

介绍到此,可以看看我学习的链接。

猜你喜欢

转载自blog.csdn.net/leon_zeng0/article/details/106935688