C/C++ 三种编译环境的使用

本文详细介绍了如何在VSCode、Dev-C++和Code::Blocks中配置C++的编译和调试环境,并对比了三者在易用性上的差异。VSCode配置较为全面,支持自动变量查看;Dev-C++需要手动设置调试信息,不支持自动生成变量查看;Code::Blocks不支持单文件编译,调试环境配置相对简单。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

放结论,三种编译环境易用性上:VS Code > Code::Blocks > Dev-C++。Dev-C++ 不支持自动生成的变量查看。Code::Blocks 不支持单文件编译。

VS Code

安装

官网下载,安装 VS Code,mingw
其中 mingw 有 setup 版本的可以一键配置好

编译调试环境配置

在项目根目录下添加以下配置文件:

.vscode\c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "gcc-x64",
            "compilerArgs": []
        }
    ],
    "version": 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": "g++.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "task g++"
        }
    ]
}

.vscode\settings.json

{
    "files.associations": {
        "*.tcc": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "exception": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "ostream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "type_traits": "cpp",
        "typeinfo": "cpp",
        "cstring": "cpp",
        "ctime": "cpp",
        "tuple": "cpp",
        "map": "cpp",
        "set": "cpp",
        "array": "cpp",
        "deque": "cpp",
        "string": "cpp",
        "vector": "cpp",
        "optional": "cpp",
        "fstream": "cpp",
        "algorithm": "cpp",
        "cerrno": "cpp",
        "climits": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "memory": "cpp",
        "queue": "cpp",
        "sstream": "cpp",
        "stack": "cpp",
        "utility": "cpp",
        "list": "cpp",
        "rope": "cpp",
        "functional": "cpp",
        "chrono": "cpp",
        "random": "cpp",
        "atomic": "cpp",
        "bitset": "cpp",
        "cfenv": "cpp",
        "charconv": "cpp",
        "cinttypes": "cpp",
        "codecvt": "cpp",
        "complex": "cpp",
        "condition_variable": "cpp",
        "csetjmp": "cpp",
        "csignal": "cpp",
        "cuchar": "cpp",
        "forward_list": "cpp",
        "unordered_map": "cpp",
        "unordered_set": "cpp",
        "iterator": "cpp",
        "memory_resource": "cpp",
        "numeric": "cpp",
        "ratio": "cpp",
        "regex": "cpp",
        "hash_map": "cpp",
        "future": "cpp",
        "iomanip": "cpp",
        "mutex": "cpp",
        "scoped_allocator": "cpp",
        "shared_mutex": "cpp",
        "thread": "cpp",
        "typeindex": "cpp",
        "valarray": "cpp",
        "stdbool.h": "c",
        "stdint.h": "c"
    }
}

.vscode\tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "task g++",
            "type": "shell",
            "command": "D:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}

Dev-C++

调试环境配置

工具栏 -> Tools -> Compiler Options -> settings -> Linker -> Generate debugging information (-g3):Yes

ps. Dev-C++ 查看变量只能一个个手动添加

Code::Blocks

项目环境配置

工具栏 -> File -> New -> Project… -> Console application -> C++ -> 项目名称 -> 默认编译器 -> Finish

每要编写一个 C++ 程序,就新建一个 .cpp 文件,编译时复制粘贴到 main.cpp 编译调试运行

ps. Code::Blocks 不支持单文件编译

编写环境配置

工具栏 -> Settings -> Editor -> Code Completion -> 能开则开

调试环境配置

工具栏 -> Settings -> Debugger -> Default:

  • Disable startup scripts (-nx) (GDB only)

取消勾选此项后,STL 的变量可以在调试时被正常观察

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值