VSCode是一个功能强大的跨平台代码编辑器,可以很好地支持C/C++项目开发。以下是使用VSCode在Windows 11上编译运行此项目的详细步骤。
在VSCode中安装以下扩展:
安装方法:点击左侧扩展图标,搜索扩展名称并安装。
按照前面的说明安装MSYS2和必要的依赖项。确保以下组件已安装:
pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-make mingw-w64-x86_64-gdb mingw-w64-x86_64-openssl
C:\msys64\mingw64\bin
C:\msys64\usr\bin
在项目根目录创建.vscode
文件夹,并在其中创建以下配置文件:
{
"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
}
{
"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": []
}
]
}
{
"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"
}
]
}
在VSCode中,可以通过以下方式编译项目:
使用任务:
Ctrl+Shift+B
调出构建任务菜单使用终端:
Ctrl+`
打开集成终端cd 1
mingw32-make -f Makefile.win
F5
或点击"运行和调试"按钮除了使用调试模式,也可以直接在终端中运行编译好的程序:
cd 1
./coap_server.exe
./coap_client.exe
找不到头文件:
c_cpp_properties.json
中的includePath配置,确保包含了所有必要的目录GDB调试器找不到:
launch.json
中的miDebuggerPath
路径,确保GDB已安装并且路径正确编译命令失败:
外部控制台不显示:
launch.json
中的externalConsole
设置为true
VSCode无法找到编译器:
c_cpp_properties.json
中正确设置了compilerPath