Installation of python and C++ debugging environment of VS code in Linux environment

Installation of python and C++ debugging environment of VS code in Linux environment


foreword

  • The recently written C++ (basic learning, cmake, etc.) and python projects use the IDE is vscode, written in the linux environment, using the cloud server.

1. Cloud server environment

insert image description here

2. VS code related information

  • Download address: https://code.visualstudio.com/download
  • List of plugins installed in VS Code:
    • Chinese installation package:
      • MS-CEINTL.vscode-language-pack-zh-hans
    • SSH remote development:
      • ms-vscode-remote.remote-ssh
      • ms-vscode-remote.remote-ssh-edit
      • ms-vscode.remote-explorer
    • C++ development
      • ms-vscode.cpptools
    • python development
      • ms-python.python
    • code completion
      • TabNine.tabnine-vscode (I use this)

3. Python development environment configuration

  • Create a conda virtual environment:
  • Write a simple test code dem.py
# python 代码调试
# 计算 1+2+3+...+100 的值

sum = 0


for i in range(101):
    
    sum+=i

  
print(sum)
  • In debugger mode, create a new .vscode folder under this file, and add a launch.json file under it
    insert image description here
  • launch.json file content
{
    
    
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
    
    
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",                   // 当前文件
            "console": "integratedTerminal",
            "justMyCode": true                      // false表示可以进入第三方库(如Pytorch)里进行调试
        }
    ]
}
  • Breakpoints for debugging
    insert image description here

Four, C++ development environment configuration

1. Test main.cpp

  • linux is compiled with g++
  • On the server terminal command line: which g++ , you can view the g++ path
  • My g++ path is as follows:
    insert image description here
  • Test code main.cpp
#include<iostream>

using namespace std;


int main()
{
    
    
    int sum {
    
    0};

    for(int i {
    
    0}; i<101; i++)
    {
    
    
        sum += i;
    }

    cout << "结果: sum=" << sum << endl;

    return 0;
}
  • Then use the VS Code menu: Terminal - Run the build task to generate an executable file, you need to add tasks.json in .vscode first
{
    
    
  "version": "2.0.0",
  "tasks": [
    {
    
    
      "type": "cppbuild",
      "label": "C/C++: g++ 生成活动文件",
      "command": "/usr/bin/g++", // g++的路径
      "args": [
        "-fdiagnostics-color=always", // 颜色
        "-g",  // 调试信息
        "-Wall", // 开启所有警告
        "-std=c++14", // c++14标准
        "${file}", // 文件本身,仅适用于C++基础知识教学,无法同时编译所有文件
        // "${fileDirname}/*.cpp", // 文件所在的文件夹路径下所有cpp文件
        "-o", // 输出
        "${workspaceFolder}/release/${fileBasenameNoExtension}" // 文件所在的文件夹路径/release/当前文件的文件名,不带后缀
      ],
      "options": {
    
    
        "cwd": "${fileDirname}" // 文件所在的文件夹路径
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
    
    
        "kind": "build",
        "isDefault": true
      },
      "detail": "编译器: /usr/bin/g++"
    }
  ]
}
  • execute cpp file
  • g++ main.cpp -o main
  • main.cpp is the main.cpp code source file to be executed main: Generate executable binary main executable file
  • ./main can get the output result

2. to debug

  • Under the linux system, gdb is needed to debug the c++ program
  • install gdb
apt-get update
apt-get install  gdb
  • launch.json is modified to:
{
    
    
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
    
    
            "name": "(gdb) 启动",
            "type": "cppdbg", // C++调试
            "request": "launch",
            "program": "${workspaceFolder}/release/${fileBasenameNoExtension}",  // 文件所在的文件夹路径/release/当前文件的文件名,不带后缀
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}", // 文件所在的文件夹路径
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
    
    
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
    
    
                    "description":  "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ 生成活动文件" // tasks.json的label
        },
        {
    
    
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}", // 当前文件
            // "program": "demo.py", // 指定文件
            "console": "integratedTerminal",
            "justMyCode": true // false表示可以进入第三方库(如Pytorch)里进行调试
        }
    ]
}

3. Perform debug program

insert image description here

4. Run the main.cpp program steps

  • click终端-运行生成任务
  • 终端命令行输入 ./release/main
  • The main.cpp program can be executed

Guess you like

Origin blog.csdn.net/m0_60890175/article/details/132089883