Visual Studio Code 下 C/C++ 开发环境搭建

在win系统 Visual Studio Code下C/C++开发环境搭建

1. 安装Visual Studio Code

在Windows环境下,可以按照以下步骤安装Visual Studio Code:
从官方网站下载Visual Studio Code安装程序。
运行安装程序,并按照提示进行安装。
安装完成后,打开Visual Studio Code即可开始使用。

2. Visual Studio Code安装相关插件

C/C++ for Visual Studio Code(必需)
C/C++ Extension Pack
C/C++ Extension UI Themes
CMake For VisualStudio Code
CMake Tools

3. MingGw安装

MingGw 是 GCC 在 Windows 平台上的移植版本(C/C++ 编译环境)

下载MingGw 解压得到 mingw64 文件夹,拷贝文件到自定义目录(例:C:\Program Files (x86)\)
添加环境变量 C:\Program Files (x86)\mingw64\bin
打开命令行窗口使用 gcc -v 和 g++ -v 验证安装是否成功

4. 安装 CMake

5. Visual Studio Code 下生成可执行文件

添加一个 main.cpp 测试文件
工具栏 -> 终端 -> 配置任务。在.vscode目录下生成tasks.json 文件并编辑
工具栏 -> 终端 -> 运行任务(或者Ctrl + Shift + B)。可以看到生成可执行文件
// tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "C:\\Program Files (x86)\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                // 这里注意变量${file},要在main.cpp 目录下运行任务,否则提示找不到cpp文件
                "${file}", 
                "-o",
                // 这里可以自定义输出目录和目标文件
                "${fileDirname}\\bin\\main.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "编译器: \"C:\\Program Files (x86)\\mingw64\\bin\\g++.exe\""
        }
    ]
}

6. Visual Studio Code 下调试程序

工具栏 -> 运行 -> 添加配置 -> C++(GDB)。在.vscode目录下生成 launch.json 文件
在 launch.json 中 添加配置 -> C/C++ GDB启动。根据本地配置修改文件后即可调试
// launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
             // 这里指向可执行程序
             "program": "${fileDirname}\\bin\\main.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            // 这里指向 mingw64-gdb 
            "miDebuggerPath": "C:\\Program Files (x86)\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            // 调试前生成可执行文件 指向 tasks.json 文件的 tasks -> label
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]
}

参考文档 https://code.visualstudio.com/docs/cpp/introvideos-cpp.js/

7. CMake 项目配置

在main.cpp 目录下添加 CMakeLists.txt文件 并编辑
新建bin文件夹
进入bin,终端执行 cmake ..(需要生成 Makefile 文件 如果生成了.sln文件,要指定g++编译器 cmake .. -G "MinGW Makefiles")
执行 mingw32-make.exe
// CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
project (main)
add_executable(main main.cpp)

8. CMake 多文件项目配置方法

文件目录
├── .vscode
│   ├── launch.json
│   └── tasks.json
├── bin
├── CMakeLists.txt
├── main.h
├── main.cpp
// CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
project(main)
add_definitions("-Wall -g")
add_executable(main main.h main.cpp)
// tasks.json
{
    "version": "2.0.0",
    "options": {
        "cwd": "${workspaceFolder}/bin"
    },
    "tasks": [
        {
            "label": "CMake",
            "type": "shell",
            "command": "cmake",
            "args": [
                ".."
            ]
        },
        {
            "label": "Make",
            "command": "mingw32-make.exe",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "args": [

            ]
        },
        {
            "label": "Build",
            "dependsOn":[
                "CMake",
                "Make"
            ]
        }

    ]
}
// launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\bin\\main.exe", 
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files (x86)\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Build"
        }
    ]
}

9. Visual Studio Code 常用文件变量

${workspaceFolder} -在VS Code中打开的文件夹的路径
${workspaceFolderBasename} -在VS Code中打开的文件夹名称,不带任何斜杠(/)
${file} -当前打开的文件
${relativeFile} -当前相对于打开的文件workspaceFolder
${relativeFileDirname} -当前打开的文件相对于的目录名workspaceFolder
${fileBasename} -当前打开的文件的基本名称
${fileBasenameNoExtension} -当前打开的文件的基本名称,没有文件扩展名
${fileDirname} -当前打开的文件的目录名
${fileExtname} -当前打开的文件的扩展名
${cwd} -启动时任务运行器的当前工作目录
${lineNumber} -活动文件中当前选择的行号
${selectedText} -活动文件中的当前选定文本
${execPath} -正在运行的VS Code可执行文件的路径
${defaultBuildTask} -默认构建任务的名称
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值