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

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

在这里插入图片描述

  1. 编写C++样例程序

该文件命名为main.cpp

#include <iostream>

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. 使用linux命令初步运行

2.1 打开终端

2.1.1 选择Terminal

2.1.2 选择New Terminal

在这里插入图片描述

2.1.3 在终端中输入如下命令

g++ .\main.cpp

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

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

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

g++ -g .\main.cpp

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"
        }
    ]
}

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": []
}

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);

3.4 创建文件swap.cpp

#include"swap.h"

void swap(int &a , int &b)
{
    int temp = a ;
    a = b;
    b = temp;
}

3.5 创建文件main.cpp

#include <iostream>
#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;
} 

3.6 打开终端,并执行命令

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

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

  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值