这里以在windows 10/11上建立开发环境为例,由于vscode IDE比较轻量级而且开源免费,支持多种高级编程语言,插件工具支持丰富,是现在广泛普及的编程IDE。在windows平台上开发c/c++程序可以使用两种编译器环境。(1)、用原生windows系统,使用开源的c++编译器,可以参考引文下载链接。(2)、用WSL的linux环境,用linux环境下的gcc,g++编译器;通过如下命令安装:sudo apt install g++ gdb make。
这里以第(2)种情况来做说明,首先要在windows系统上安装好WSL。具体安装方法可以参考引文。
在vscode IDE中要按ctrl+shift+p弹出”WSL:Connect to WSL using Distro…. “,选择安装的WSL对应的ubuntu版本,然后打开特定的工程目录文件,在vscode当前打开的根目录下建立.vscode目录并创建launch.json文件。内容如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: g++.exe 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"miDebuggerArgs": "-q -ex quit; wait() { fg >/dev/null; }; /bin/gdb -q --interpreter=mi",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file"
}
]
}
在.vscode同一级目录下建立tasks.json。内容如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: /usr/bin/g++"
},
{
"label": "build",
"type": "shell",
//"command": "cd ${workspaceFolder}/${fileDirname}/build ;cmake ../ ;make",
"command": "cd ${fileDirname}; mkdir build ; cd build; cmake ../ ;make",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "clean",
"type": "shell",
"command": "make clean"
},
]
}
配置好上面的json文件后,打开特定的c/c++程序,按F5就可以执行编译调试执行了,还可以通过F9设置断点跟踪代码状态。如果要编译目录下的所有cpp文件,可以将args数组里边的项设为 "*.cpp"。
如果用windows原生系统上做c/c++开发,command的g++的路径要设置成windows系统中的c++编译器的路径。gdb同理进行设置。
如果用cmake、makefile等工具去构建更大的程序,则需要安装对应的插件和程序。并需要熟悉相关的构建脚本的编写,这里暂时不做说明。
References
Leave a Reply