记录自己在vscode使用过程中环境配置遇到的问题
常用插件
配置C++环境
vsocde的环境配置主要放在4个文件中
c_cpp_properties.json
launch.json
settings.json
task.json
ctrl+shift+p
打开c_cpp_properties.json主要是用来配置include的头文件路径和使用的C++标准
{
"configurations": [
{
"name": "Linux",
"includePath": [ // 头文件路径
"${workspaceFolder}/**" ,
// "${workspaceFolder}/include",
// "${workspaceFolder}/lib/SOEM/soem",
"${workspaceFolder}/SOEM/install/include"
],
"defines": [],
"compilerPath": "/usr/bin/gcc", // 告诉系统编译器地址
"cStandard": "c89", // 告诉系统对C文件使用的C标准
"cppStandard": "c++17", // 告诉系统对CPP文件使用的C++标准
"intelliSenseMode": "linux-gcc-x64" // 不是太清楚,大概是告诉系统智能识别的模式。
}
],
"version": 4
}
打开task.json
{
"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",
"${workspaceFolders}/build/main.o",
"-L",
"${workspaceFolders}/SOEM/install/lib/", // 指向 DLL 所在的目录
"-l",
"libsoem.a"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}
其中args中的-L和-l表示链接库所在位置
相当于g++ -o main.o -L -l -g
打开launch.json
{
//Use Intellisense to learn about possible attributes.
//Hover to view descriptions of existing attributes.
//For more information,visit: https://go.microsoft.com/fwlink/?linkid=830387
"version":"0.2.0",
"configurations":[
{
"name":"(gdb) 启动", //name随便取,用来在定义了多个configuration的时候区分执行的是哪一套参数。
"type":"cppdbg", //这个一般就不要改了,这个我还不了解其他的参数配置
"request":"launch", //这个一般就不要改了,这个我还不了解其他的参数配置
"program":"${workspaceFolder}/src/build/test_ethercat.o", //执行这个文件,相当于命令行中输入这行
"args":[], //program的参数,相当于命令行中在program后输入用空格隔开的这些参数。我这里前两个是执行文件自带的参数设置,后三个是将标准输出和标准错误输入到文件out中。
"stopAtEntry":true, //大概是表示是否在main函数处停下,类似在main上打断点。
"cwd":"${fileDirname}", //表示在哪个目录下执行program,相当于命令行中输入cd 该行
"environment":[], //不知道干嘛的
//"externalConsole":true, //如果使用控制台则需要打开这个。我这边加了这句后会有不知道在使用哪个控制台的问题,反正会很奇怪,就注释掉了。
"MIMode":"gdb", //使用哪个工具作为调试工具,只有两个选择gdb或lldb。lldb我不了解,和我一样不了解的人就写gdb吧。
// "miDebuggerPath":"/usr/bin/gdb", //调试工具的路径
"setupCommands":[ //gdb相关的一些设置命令,相当于在执行gdb后,对gdb下命令行的设置。gdb相关的初始化配置,要在~/.gdbinit文件中配置,或者在这配置,我另开一篇文章写,涉及下面enable-pretty-printing的,不一定开了就有用了,还涉及其他的问题,这里先放个TODO。
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
// "internalConsoleoptions":"openOnSessionstart", //不知道干嘛的
// "preLaunchTask":"C/C++: g++ build active file" //执行program前需要执行的task,这里需要和task.json中要执行的task的label一致
// "preLaunchTask":"" //如果没有tasks.json,或者不需要执行前置task,则可以这样写,或不写这行。这种情况一般是你已经有了带调试信息的可执行文件,不要在再通过编译等操作得到了。
}
]
}
注意其中注释的"preLaunchTask":"C/C++: g++ build active file"如果是通过task.json条是这里的名字要和task.json中的label值一致