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

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

VSCode配置C++环境:g++篇_第1张图片

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

2.1 打开终端

2.1.1 选择Terminal

2.1.2 选择New Terminal

VSCode配置C++环境:g++篇_第2张图片

2.1.3 在终端中输入如下命令

g++ .\main.cpp

VSCode配置C++环境:g++篇_第3张图片
注意
(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文件

VSCode配置C++环境:g++篇_第4张图片
生成如下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中内容复制粘贴后,须修改如下字段:

VSCode配置C++环境:g++篇_第5张图片

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

直接按F5键亦可开始调试程序
VSCode配置C++环境:g++篇_第6张图片

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 
#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执行程序前,注意打上断点

你可能感兴趣的:(笔记,vscode,c++,ide)