使用VSCode在Windows 11上编译运行项目
使用VSCode在Windows 11上编译运行项目
VSCode是一个功能强大的跨平台代码编辑器,可以很好地支持C/C++项目开发。以下是使用VSCode在Windows 11上编译运行此项目的详细步骤。
1. 安装VSCode
- 访问VSCode官网下载并安装VSCode
- 安装完成后,启动VSCode
2. 安装必要的VSCode扩展
在VSCode中安装以下扩展:
- C/C++ (由Microsoft提供)
- C/C++ Extension Pack (包含调试工具等)
- Makefile Tools (用于支持Makefile项目)
安装方法:点击左侧扩展图标,搜索扩展名称并安装。
3. 配置MSYS2/MinGW环境
按照前面的说明安装MSYS2和必要的依赖项。确保以下组件已安装:
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
- 打开Windows设置 > 系统 > 关于 > 高级系统设置 > 环境变量
- 在"系统变量"部分找到并编辑"Path"变量
- 添加以下路径:
C:\msys64\mingw64\bin C:\msys64\usr\bin
- 点击"确定"保存更改
5. 在VSCode中打开项目
- 启动VSCode
- 选择"文件" > “打开文件夹”
- 导航到项目根目录并打开
6. 创建VSCode配置文件
在项目根目录创建.vscode
文件夹,并在其中创建以下配置文件:
c_cpp_properties.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
{"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
{"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中,可以通过以下方式编译项目:
-
使用任务:
- 按下
Ctrl+Shift+B
调出构建任务菜单 - 选择"build all"任务编译所有组件
- 或选择特定任务编译单个组件(如"build server")
- 按下
-
使用终端:
- 按下
Ctrl+`
打开集成终端 - 导航到项目目录:
cd 1
- 执行编译命令:
mingw32-make -f Makefile.win
- 按下
8. 调试项目
- 打开需要调试的源代码文件
- 在代码中设置断点(点击行号左侧)
- 按下
F5
或点击"运行和调试"按钮 - 从下拉菜单中选择相应的调试配置:
- “Debug Server”:调试服务端
- “Debug Client”:调试客户端
- “Debug Protocol Test”:调试协议测试程序
9. 运行程序
除了使用调试模式,也可以直接在终端中运行编译好的程序:
- 打开VSCode集成终端
- 导航到编译目录:
cd 1
- 运行服务端:
./coap_server.exe
- 在新终端中运行客户端:
./coap_client.exe
10. 可能遇到的VSCode相关问题
-
找不到头文件:
- 问题:IntelliSense无法找到头文件
- 解决方案:检查
c_cpp_properties.json
中的includePath配置,确保包含了所有必要的目录
-
GDB调试器找不到:
- 问题:启动调试时提示找不到GDB
- 解决方案:检查
launch.json
中的miDebuggerPath
路径,确保GDB已安装并且路径正确
-
编译命令失败:
- 问题:构建任务失败
- 解决方案:检查MSYS2/MinGW是否正确安装,PATH环境变量是否正确设置
-
外部控制台不显示:
- 问题:调试时外部控制台未启动
- 解决方案:确保
launch.json
中的externalConsole
设置为true
-
VSCode无法找到编译器:
- 问题:编译时报错找不到编译器
- 解决方案:确认GCC已安装,并在
c_cpp_properties.json
中正确设置了compilerPath