VS code 配置流程

此文档介绍在Windows环境,通过vs code进行文件编写、配置、编译及调试过程。

文件编写:首先下载编译器,Windows x86环境下可以用mingw,arm linux环境下需根据板子的型号下载交叉编译工具链,将安装包的bin目录添加到Windows环境变量path中。

 

 

之后就可以创建一个新建文件夹命名为newfile,用于存放所写的代码文件和生成的配置文件。然后打开vs code,选择文件->打开文件夹->newfile,点击打开文件编写代码,如图所示:

 

文件配置:首先点击查看->调试,发现显示“没有配置”,可以点击旁边的齿轮,选择C++(GDB/LLDB),之后会看到“gcc.exe build and debug active file(x86下调试)”和“default configuration(linux系统下调试)”。

情况1-如果是在x86系统进行编译,选择“gcc.exe build and debug active file”,此时根据提示打开launch.json文件进行配置,配置如下:

{

// 使用 IntelliSense 了解相关属性。

// 悬停以查看现有属性的描述。

// 欲了解更多信息,请访问:https://go.microsoft.com/fwlink/?linkid=830387

"version": "0.2.0",

"configurations": [

{

         "name": "gcc.exe build and debug active file",//用户可以自己定义      "type": "cppdbg",   //配置类型,这里只能为cppdbg

         "request": "launch", //请求配置类型,可以为launch或attach

         "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",//将要进行调式程序的路径

         "args": [],  // 程序调试时传递给程序的命令行参数,一般设为空即可

         "stopAtEntry": false,  // 设为true时程序将暂停在程序入口处

         "cwd": "${workspaceFolder}", // 调试程序时的工作目录

         "environment": [],

         "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台

         "MIMode": "gdb",  //指定连接的调试器

         "miDebuggerPath": "C:\\APP\\MinGW\\mingw32\\bin\\gdb.exe",// 调试器路径,Windows下后缀不能省略,Linux下则去掉

         "setupCommands": [ //模板

             {

                 "description": "Enable pretty-printing for gdb",

                 "text": "-enable-pretty-printing",

                 "ignoreFailures": true

              }

          ],

          "preLaunchTask": "gcc.exe build active file"  // 调试会话开始前执行的任务,一般为编译程序。与tasks.json的label相对应

       }

  ]

}

需要注意的是Program存放的是编译好后的可执行文件路经,miDebuggerPath为gdb.exe存放的路径,其他的内容可以不用修改。

然后打开tasks.json文件,task其实就类似于makefile,根据指定的规则build源文件,配置如下:

{

   "tasks": [

     {

         "type": "shell",

       "label": "gcc.exe build active file",

       "command": "C:\\APP\\MinGW\\mingw32\\bin\\gcc.exe",

       "args": [

        "-g",

         "${file}",

         "-o",

         "${fileDirname}\\${fileBasenameNoExtension}.exe"

       ],

       "options": {

         "cwd": "C:\\APP\\MinGW\\mingw32\\bin"

       }

     }

   ],

   "version": "2.0.0"

}

其中label需与launch.json下的perLaunchTask内容一致,command为编译器gcc.exe路径,cwd为mingw的bin路径。

最后设置断点,填写监控变量,点击调试->启动调试,就可以通过vs code进行代码调试了。

情况2-如果需要在linux系统下进行调试,选择“default configuration”,弹出launch.json,修改内容如下:

{

 // 使用 IntelliSense 了解相关属性。

 // 悬停以查看现有属性的描述。

 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387

 "version": "0.2.0",

 "configurations": [   

  {

            "name": "(gdb) ARM remote debug",

            "type": "cppdbg",

            "request": "launch",

            "program": "C:\\vs code pro\\test1", //调试的文件路径

            "args": [],

            "stopAtEntry": false,

            "cwd": "${workspaceFolder}",

            "environment": [],

            "externalConsole": true,

            "MIMode": "gdb",   

            "miDebuggerPath":"C:\\eclipse\\gcc-arm-none\\bin\\arm-linux-gnueabihf-gdb.exe",

            //交叉编译工具器gdb路径

            "miDebuggerServerAddress": "192.168.0.10:2019", //目标板IP地址和端口号,端口号可随意,但需与目标板输入一致

            "setupCommands": [

                {

                    "description": "Enable pretty-printing for gdb",

                    "text": "-enable-pretty-printing",

                    "ignoreFailures": true

                }

            ]

        }

   ]

}

同样要注意的是Program存放的是编译好后的可执行文件路经,miDebuggerPath为gdb.exe存放的路径,另外还需添加一行miDebuggerServerAddress为目标板的IP地址和端口号。

在vs code终端用交叉编译器编译出可执行文件,再将需要调式的可执行文件和交叉编译工具链中gdbserver文件拷贝进目标板,目标板中输入./gdbserver 192.168.0.100:2019 ./test,等待主机连接。在vs code点击调试->启动调试,则可以与目标板连接,进行远程调试。

发布了9 篇原创文章 · 获赞 5 · 访问量 2206

猜你喜欢

转载自blog.csdn.net/CSDN_liu_sir/article/details/101015565