使用VSCode在Windows 11上编译运行项目

使用VSCode在Windows 11上编译运行项目

VSCode是一个功能强大的跨平台代码编辑器,可以很好地支持C/C++项目开发。以下是使用VSCode在Windows 11上编译运行此项目的详细步骤。

1. 安装VSCode

  1. 访问VSCode官网下载并安装VSCode
  2. 安装完成后,启动VSCode

2. 安装必要的VSCode扩展

在VSCode中安装以下扩展:

  1. C/C++ (由Microsoft提供)
  2. C/C++ Extension Pack (包含调试工具等)
  3. Makefile Tools (用于支持Makefile项目)

安装方法:点击左侧扩展图标,搜索扩展名称并安装。

3. 配置MSYS2/MinGW环境

按照前面的说明安装MSYS2和必要的依赖项。确保以下组件已安装:

bash 复制代码
pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-make mingw-w64-x86_64-gdb mingw-w64-x86_64-openssl

4. 将MSYS2/MinGW添加到系统PATH

  1. 打开Windows设置 > 系统 > 关于 > 高级系统设置 > 环境变量

  2. 在"系统变量"部分找到并编辑"Path"变量

  3. 添加以下路径:

    复制代码
    C:\msys64\mingw64\bin
    C:\msys64\usr\bin
  4. 点击"确定"保存更改

5. 在VSCode中打开项目

  1. 启动VSCode
  2. 选择"文件" > "打开文件夹"
  3. 导航到项目根目录并打开

6. 创建VSCode配置文件

在项目根目录创建.vscode文件夹,并在其中创建以下配置文件:

c_cpp_properties.json
json 复制代码
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/1/**",
                "${workspaceFolder}/NIST-PQ-Submission-Kyber-20201001/Optimized_Implementation/crypto_kem/kyber768/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:/msys64/mingw64/bin/gcc.exe",
            "cStandard": "c99",
            "cppStandard": "c++14",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}
tasks.json
json 复制代码
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build server",
            "type": "shell",
            "command": "cd ${workspaceFolder}/1 && mingw32-make -f Makefile.win coap_server.exe",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        },
        {
            "label": "build client",
            "type": "shell",
            "command": "cd ${workspaceFolder}/1 && mingw32-make -f Makefile.win coap_client.exe",
            "group": "build",
            "problemMatcher": [
                "$gcc"
            ]
        },
        {
            "label": "build protocol test",
            "type": "shell",
            "command": "cd ${workspaceFolder}/1 && mingw32-make -f Makefile.win protocol_test.exe",
            "group": "build",
            "problemMatcher": [
                "$gcc"
            ]
        },
        {
            "label": "build all",
            "type": "shell",
            "command": "cd ${workspaceFolder}/1 && mingw32-make -f Makefile.win",
            "group": "build",
            "problemMatcher": [
                "$gcc"
            ]
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "cd ${workspaceFolder}/1 && mingw32-make -f Makefile.win clean",
            "group": "build",
            "problemMatcher": []
        }
    ]
}
launch.json
json 复制代码
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Server",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/1/coap_server.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/1",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build server"
        },
        {
            "name": "Debug Client",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/1/coap_client.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/1",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build client"
        },
        {
            "name": "Debug Protocol Test",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/1/protocol_test.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/1",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build protocol test"
        }
    ]
}

7. 编译项目

在VSCode中,可以通过以下方式编译项目:

  1. 使用任务:

    • 按下Ctrl+Shift+B调出构建任务菜单
    • 选择"build all"任务编译所有组件
    • 或选择特定任务编译单个组件(如"build server")
  2. 使用终端:

    • 按下``Ctrl+```打开集成终端
    • 导航到项目目录:cd 1
    • 执行编译命令:mingw32-make -f Makefile.win

8. 调试项目

  1. 打开需要调试的源代码文件
  2. 在代码中设置断点(点击行号左侧)
  3. 按下F5或点击"运行和调试"按钮
  4. 从下拉菜单中选择相应的调试配置:
    • "Debug Server":调试服务端
    • "Debug Client":调试客户端
    • "Debug Protocol Test":调试协议测试程序

9. 运行程序

除了使用调试模式,也可以直接在终端中运行编译好的程序:

  1. 打开VSCode集成终端
  2. 导航到编译目录:cd 1
  3. 运行服务端:./coap_server.exe
  4. 在新终端中运行客户端:./coap_client.exe

10. 可能遇到的VSCode相关问题

  1. 找不到头文件:

    • 问题:IntelliSense无法找到头文件
    • 解决方案:检查c_cpp_properties.json中的includePath配置,确保包含了所有必要的目录
  2. GDB调试器找不到:

    • 问题:启动调试时提示找不到GDB
    • 解决方案:检查launch.json中的miDebuggerPath路径,确保GDB已安装并且路径正确
  3. 编译命令失败:

    • 问题:构建任务失败
    • 解决方案:检查MSYS2/MinGW是否正确安装,PATH环境变量是否正确设置
  4. 外部控制台不显示:

    • 问题:调试时外部控制台未启动
    • 解决方案:确保launch.json中的externalConsole设置为true
  5. VSCode无法找到编译器:

    • 问题:编译时报错找不到编译器
    • 解决方案:确认GCC已安装,并在c_cpp_properties.json中正确设置了compilerPath
相关推荐
沐飞工具箱44 分钟前
长视频自动切片的技术逻辑:如何结合语音、画面与时间轴识别真正的内容高光?
ide·macos·音视频·xcode·用户运营·内容运营
Answer1st5 小时前
【FPGA学习】VSCode 写 Verilog 操作指南(XII)
vscode·学习·fpga开发
皓月盈江6 小时前
Windows 复制文件夹到 Linux 后显示“带锁”解决方法
linux·运维·windows·linux文件·文件带锁·文件夹带锁
byte轻骑兵6 小时前
异构混合环境远程控制工具适配对比(Windows / Mac / Linux / 国产系统 / arm64 / 鸿蒙)
linux·windows·macos·远程办公
卓豪终端管理9 小时前
MDM 落地实战:一台设备从注册到退役的四道关
运维·网络·windows·安全
Star4zure1 天前
DirectX Repair 下载与使用完全指南:游戏 DLL 丢失与系统故障修复
windows·directx·系统修复·修复工具·directx repair·dll 缺失·游戏启动报错修复
夏日、荷花&你1 天前
Windows 11 下 Codex CLI 与 DeepSeek Harness(DSH)安装配置教程
windows
在角落发呆1 天前
sqlserver 如何备份数据库:从手动备份到自动定时、异地容灾的完整方案
数据库·windows·备份·数据备份
warpdrivelabs1 天前
主流 Agent 的 Windows 支持调研与 cmx-agent 适配方案
windows·去中心化
在角落发呆1 天前
sql创建备份表全攻略:从 SELECT INTO 到跨机容灾
数据库·windows·sql·自动化·备份