1.配置vscode
vscode中的插件没有包含编译器和调试工具,在使用前需要自己安装。配置文件所在的.vscode
文件仅针对当前文件夹有效,创建其他文件夹若无特殊需求只需要复制过去即可。若是在当前文件夹下子文件夹中又配置了.vscode
文件,子路径.vscode
中的参数会覆盖根目录下.vscode
中的参数。
C/C++环境配置:
-
配置编译环境:
为当前工作目录配置编译环境,我们需要创建一个
tasks.json
文件来告诉VS Code如何来编译源文件,选择Terminal --> Configure Tasks
,然后点击C/C++: g++ build active file
,会自动生成一个tasks.json
,根据自己的需求来修改,tasks.json
是为整个目录配置环境,不需要修改诸如 “${fileDirname}” 这样的变量,“type”
可以改成“shell”
,但不能任意修改;“label”
是task
的名称,可以随意,但要与launch.json
文件中的变量“preLaunchTask”
设置一致;“command”
来指定编译器名,可以不带绝对路径。官方文档关于modbus库、xlsxwrite库使用遇到的问题:
-
g++-11
和g++
是两个编译器,在tasks.json
的command
参数中定义,此处选择为g++
,点击菜单栏”运行“
即使用g++
进行编译和执行。在group
中将isDefault
,表示g++
为默认编译器,下次点击右上角”运行C/C++任务“
默认按照此处定义的task.json
执行编译任务。 -
由于此处使用了第三方库
libmodbus
和libxlsxwriter
,虽然我已经下载到了Linux下usr/lib
和usr/include
中,此处还是需要显式的在参数args
中注明"-lmodbus"
和"-lxlswriter"
,此时编译的效果就与在命令行中使用g++
进行编译的命令一致,可以正常执行。如果将"-lmodbus"
和"-lxlswriter"
注释掉,代码无法正常编译。 -
执行上述步骤使我在终端成功运行代码,但是在使用Code Runner插件运行时出现了同样的报错,因此我断定是Code Runner在执行编译命令时也未注明库
"-lmodbus"
和"-lxlswriter"
,在“管理/设置/Code Runner: Excutor Map/在settings.json中设置”
将cpp
参数设置为:"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -lxlsxwriter -lmodbus && $dir$fileNameWithoutExt"
,此时可以通过Code Runner运行源文件。 -
有时需要使用Code Runner同时运行多个
.cpp
文件,点击“管理/设置/Code Runner: Excutor Map/在settings.json中设置”
,将"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt -lmodbus -lxlsxwriter",
中的$fileName
改为*.cpp
即可。 -
除了在
“管理/设置/Code Runner: Excutor Map/在settings.json中设置”
中更改之外,也可以在.vscode
目录下添加一个setting.json
文档,按照如下代码进行设置,在执行时,.vscode
目录下setting.json
中命令的优先级高于“管理/设置/Code Runner: Excutor Map/在settings.json中设置”
中的命令。// .vscode/setting.json { "code-runner.executorMap": { "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -lxlsxwriter -lmodbus && $dir$fileNameWithoutExt" } }
// tasks.json { "version": "2.0.0", "tasks": [ { "type": "cppbuild", "label": "g++ build", "command": "/usr/bin/g++", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}", "-lmodbus", "-lxlsxwriter" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": "build", //"group": { //"kind": "build", //"isDefault": true //}, "detail": "编译器: /usr/bin/g++" } ] }
-
-
配置调试环境:
创建一个
launch.json
文件来配置调试环境,选择Run --> Add Configuration
,会自动生成一个launch.json
,点击右下角的Add Configuration
来选定要加入的配置C/C++: (gdb) Launch
(VS Code版本不同,方式有些变化),“program”
表示需要调试的文件,下面设置为工作目录 {fileDirname} 中的 {fileBasenameNoExtension} ,当测试文件处于打开状态时,编译得到的test会被调试,“stopAtEntry”
默认为false
, 运行调试时,debugger不会在源文件中添加断点,设置为true时,调试会在main函数入口处等待。调试步骤:
- 先打断点,然后执行
调试C/C++文件
命令 - TBC
// launch.json { // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "preLaunchTask": "g++ build", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
- 先打断点,然后执行
-
补充配置:
想要进行更多的配置,例如设置编译器路径、改变C++标准等,可以创建一个
c_cpp_properties.json
文件,使用ctrl+shift+P
打开命令搜索,找到并选择C/C++:Edit Configurations(JSON)
,会自动创建一个c_cpp_properties.json
文件,按自己需要修改变量即可。includePath
参数指定了头文件的路径,在Linux中包管理器下载的一般在usr目录下,使用git下载的一般在自定义目录下,两种都需要在此处注明,${workspaceFolder}/
表示在当前工作目录下找头文件,一般默认都会有。// c_cpp_properties.json { "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "/usr/include/", "/usr/include/xlsxwriter", "/usr/include/modbus", "/usr/lib/", "/usr/local/include/", "/usr/local/lib" ], "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "c17", "cppStandard": "gnu++17", "intelliSenseMode": "linux-gcc-x64" } ], "version": 4 }
-
设置配置:
setting.json
的配置在配置编译环境部分已经说明