• VSCode配置C++环境:g++篇


    0 为VsCode安装插件C/C++

    在这里插入图片描述

    1. 编写C++样例程序

    该文件命名为main.cpp

    #include 
    
    using namespace std;
    
    int main(int argc , char ** argv)
    {
        int value1 = 10 ;
        int value2 = 20;
    
        cout << "Before Swap : " << endl;
        cout << "value1 = " << value1 << "\t value2 = " << value2 << endl;
    
        swap( value1 , value2 );
    
        cout << "After Swap : " << endl;
        cout << "value1 = " << value1 << "\t value2 = " << value2 << endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    1. 使用linux命令初步运行

    2.1 打开终端

    2.1.1 选择Terminal

    2.1.2 选择New Terminal

    在这里插入图片描述

    2.1.3 在终端中输入如下命令

    g++ .\main.cpp
    
    • 1

    在这里插入图片描述
    注意
    (1) 只输入g++ 文件名,会默认生成a.exe可执行文件
    (2) 若想指定生成exe文件名,须加参数-o,如:

    g++ .\main.cpp -o main.exe
    # 或者
    g++ .\main.cpp -o [指定文件名(不加文件扩展名亦可)]
    
    • 1
    • 2
    • 3

    (3) 若想生成带有调试信息的exe文件,须在g++后指定参数-g,如:

    g++ -g .\main.cpp
    
    • 1

    2.2 调试代码

    2.2.1 如下图所示,三步生成launch.json文件

    在这里插入图片描述
    生成如下launch.json文件:(文件内容如下)

    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "name" : "g++.exe - 生成和调试活动文件" ,
                "type": "cppdbg" ,
                "request": "launch",
                "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "gdb",
                "miDebuggerPath": "D:\\Server\\MinGW\\bin\\gdb.exe",
                "setupCommands": [
                    {
                        "description": "为 gdb 启用整齐打印",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask": "C/C++: g++.exe build active file"
            }
        ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    2.2.2 若2.2.1生成的launch文件如下所示,可将2.2.1中内容复制粘贴[若launch.json文件正常,无须看2.2.2和2.2.3]

    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": []
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.2.3 将2.2.1中内容复制粘贴后,须修改如下字段:

    在这里插入图片描述

    2.2.3 如下图所示,依次点击 > ①Run > ②Start Debugging,即可开始调试[注意打上断点]

    直接按F5键亦可开始调试程序
    在这里插入图片描述

    3 C++项目文件

    3.1 创建C++项目文件夹

    此处文件夹命名为TestProject

    3.2 使用VSCode打开该项目文件夹

    3.3 创建文件swap.h

    void swap(int &a , int &b);
    
    • 1

    3.4 创建文件swap.cpp

    #include"swap.h"
    
    void swap(int &a , int &b)
    {
        int temp = a ;
        a = b;
        b = temp;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3.5 创建文件main.cpp

    #include 
    #include "swap.h"
    using namespace std;
    
    int main(int argc , char ** argv)
    {
        int value1 = 10 ;
        int value2 = 20 ;
    
        cout << "Before Swap : " << endl;
        cout << "value1 = " << value1 << "\t value2 = " << value2 << endl;
    
        swap( value1 , value2 );
    
        cout << "After Swap : " << endl;
        cout << "value1 = " << value1 << "\t value2 = " << value2 << endl;
    
        return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3.6 打开终端,并执行命令

    g++ -g .\main.cpp .\swap.cpp -o multi_swap
    
    • 1

    3.7 生成launch.json文件

    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "name" : "g++.exe - 生成和调试活动文件" ,
                "type": "cppdbg" ,
                "request": "launch",
                // "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "program": "${workspaceFolder}\\multi_swap.exe",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "gdb",
                "miDebuggerPath": "D:\\Server\\MinGW\\bin\\gdb.exe",
                "setupCommands": [
                    {
                        "description": "为 gdb 启用整齐打印",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                // "preLaunchTask": "C/C++: g++.exe build active file"
            }
        ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    注意
    (1) 此处由于是多文件编译,故须修改program字段的值 :
    ① 首先查看cwd的值,[ 是 当前工作目录空间 (即当前项目根目录TestProject下)]
    ② 复制cwd的值至program,并在其后指明3.6命令生成exe文件的名称(全称,扩展名不可省略)
    (2) 由于已在3.6命令编译链接生成exe文件,且无Tasks.json文件,故将PreLaunchTask注释
    (3) 按F5执行程序前,注意打上断点

  • 相关阅读:
    SpringBoot 启动原理
    C#类与类库调用注意事项
    【计算机视觉|人脸建模】学习从图像中回归3D面部形状和表情而无需3D监督
    【算法】priority_queue在力扣题中的应用 | 力扣692 | 力扣347 | 力扣295 【超详细的注释和算法解释】
    副业该怎么选择,适合新手的四个副业项目,零基础也可操作的兼职
    类和对象(末)
    CommonsCollection7反序列化链学习
    ggkegg | 用这个神包玩转kegg数据库吧!~(一)
    Android13将Settings移植到AndroidStudio中
    1052 Linked List Sorting
  • 原文地址:https://blog.csdn.net/m0_48275578/article/details/126681109