当前位置: 首页 > news >正文

使用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和必要的依赖项。确保以下组件已安装:

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
{"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中,可以通过以下方式编译项目:

  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

相关文章:

  • Mybatis标签使用 -association 绑定对象,collection 绑定集合
  • 【背包dp----01背包】例题三------(标准的01背包+变种01背包1【恰好装满背包体积 产生的 最大价值】)
  • 【ROS2】Nav2源码之行为树定义、创建、加载
  • 【论文阅读】Attentive Collaborative Filtering:
  • 缓存替换算法与存储器管理的分页、分段、段页式管理联系
  • 培训机构用的教务系统
  • 如何延长电脑使用寿命?
  • 神经网络在模式识别中的应用:从语音到视觉的智能解析
  • react中的用法——setDisabled dva dispatch effects
  • 更新编译器到ARM compiler6,很多报错问题
  • FPGA 不兼容故障及处理
  • 人工智能数据标注服务规范
  • SSA-CNN+NSGAII+熵权TOPSIS,附相关气泡图!
  • Excel点击单元格内容消失
  • Spark应用部署模式实例
  • 力扣刷题Day 37:LRU 缓存(146)
  • 双线性配对
  • 从创意到变现:独立创造者的破局之路——解码《Make:独立创造者手册》
  • NetBox Docker 全功能部署方案(Ubuntu 22.04 + Docker)
  • 发行基础:Epic平台上传流程
  • “犍陀罗艺术与亚洲文明”在浙大对外展出
  • 巴西总统卢拉昨晚抵达北京
  • 新华时评:中国维护国际经贸秩序的立场坚定不移
  • 卢正已任上海市司法局党委委员、副局长
  • 领证不用户口本,还需哪些材料?补领证件如何操作?七问七答
  • 马上评丨维护学术诚信别陷入“唯AI检测”误区