C++(Hello World)

Hello World

1. 安装插件

2. 安装编译器 “MinGW"

https://cloud.tencent.com/developer/article/1944563
https://sourceforge.net/projects/mingw-w64/files/

3. 配置 includePath

g++ -v -E -x c++ -

将路径都粘贴过来

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "F:/MinGW/x86_64-7.3.0-release-win32-seh-rt_v5-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++",
                "F:/MinGW/x86_64-7.3.0-release-win32-seh-rt_v5-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/x86_64-w64-mingw32",
                "F:/MinGW/x86_64-7.3.0-release-win32-seh-rt_v5-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/backward",
                "F:/MinGW/x86_64-7.3.0-release-win32-seh-rt_v5-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/include",
                "F:/MinGW/x86_64-7.3.0-release-win32-seh-rt_v5-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/include-fixed",
                "F:/MinGW/x86_64-7.3.0-release-win32-seh-rt_v5-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "cl.exe",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}

任务的 JSON 文件

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "F:\\MinGW\\x86_64-7.3.0-release-win32-seh-rt_v5-rev0\\mingw64\\bin\\gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"    // 现在仅仅是编译一个文件
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

修改成编译当前目录下的所有 .c 文件

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "F:\\MinGW\\x86_64-7.3.0-release-win32-seh-rt_v5-rev0\\mingw64\\bin\\gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "*.c",  //  当前目录下的所有 .c 文件
                "-o",
                "${fileDirname}\\a.exe"    // 统一生成一个可执行程序名称
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

4. VSCode 调试程序

单个文件

多个文件


点击创建 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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            // "program": "enter program name, for example ${workspaceFolder}/a.exe",  //  需要调试的可执行程序
            "program": "${fileDirname}\\a.exe",  //  需要调试的可执行程序
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            // "miDebuggerPath": "/path/to/gdb",   //  gdb 位置
            "miDebuggerPath": "F:\\MinGW\\x86_64-7.3.0-release-win32-seh-rt_v5-rev0\\mingw64\\bin\\gdb.exe",   //  gdb 位置
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

# max.h
int Max(int a, int b);
# max.c
int Max(int a, int b){
    return a>b? a : b;
}
# test.c
#include <stdio.h>
#include "max.h"    //  引用自己的头文件(使用双引号)
int main(){
    int a = 10;
    int b = 100;
    int c = Max(a, b);
    printf("%d\n", c);
    printf("hello world!!!");
    return 1;
}

运行一下:

5. 使用不同文件夹来管理不同的项目

6. 配置 C++【注意编译器为:g++】

7. C++ 配置文件 task.json 设置

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "F:/MinGW/x86_64-7.3.0-release-win32-seh-rt_v5-rev0/mingw64/bin/g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${workspaceFolder}/*.cpp",  //  1. 编译当前文件夹下所有 .cpp 后缀的
                "-o",
                "${workspaceFolder}\\${workspaceRootFolderName}.exe"    //  2. ${workspaceRootFolderName}:当前工作空间根文件夹的名称
            ],
            "options": {
                "cwd": "F:/MinGW/x86_64-7.3.0-release-win32-seh-rt_v5-rev0/mingw64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}
posted @   爱新觉罗LQ  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示