VSCode 同时调试2个或多个程序

在上文:VS Code 在Linux下IDE开发C++的HelloWorld 中介绍了VS Code 的基本调试,但那只是一个应用。

在开发客户服务程序,或通讯程序中,有必要同时调试2个或多个程序,这就是本文要介绍的内容。

本文参考学习链接:https://code.visualstudio.com/docs/editor/debugging#_compound-launch-configurations

本文采用的2个代码例子来自:linux, petalinux 下的udp 通讯实验,2个非常简单的通讯例子,你也可以自己写2个简单例子代码。

编译2个程序

调式程序前需要编译程序,这里需要编译2个程序。

首先,我们要编译调试好第一个程序,比如client.c,然后添加第二个程序的编译代码。最后tasks.json是如下:

{
	"version": "2.0.0",
	"tasks": [

		{
			"type": "shell",
			"label": "C/C++: gcc-7 build active file",
			"command": "/usr/bin/gcc-7",
			"args": [
				"-g",
				"server.c",
				"-o",
				"server"
			],
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			}
		},
		{
			"type": "shell",
			"label": "C/C++: gcc-7 build active file",
			"command": "/usr/bin/gcc-7",
			"args": [
				"-g",
				"client.c",
				"-o",
				"client"
			],
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			}
		}
	]
}

其实就是复制一下,然后修改下名字。

准备调试2个程序

最开始都是编译调试好第一个程序,有了基本的launch.json, 然后再复制添加再修改一下。这里稍多一步,就是还加了一个compound。一个更好的操作就是Add Configuration,打开launch.json就可以在编辑框里右下方看到这个按钮,点击按钮添加。所谓的修改就是启动程序名字,和命令行参数。最后,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": "Server",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/server",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        {
            "name": "Client",
            "type": "cppdbg",
            "request": "launch",
            "program": "client",
            "args": ["127.0.0.1"],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ],
    "compounds": [
      {
        "name": "Server/Client",
        "configurations": ["Server", "Client"],
        //"preLaunchTask": "${defaultBuildTask}"
      }
    ]
}

调试2个程序

调试程序,可以菜单Run>Start Debugging启动,但具体调试哪个,由下面面板里选择确定。也可以选择好后,点选择栏左边的运行(或者叫调试快捷键)。下图中有3个选择。

那么你怎么控制调试哪个程序呢?看上图中CALL STACK ,你点Server ,控制按钮就对Server 起作用。出现的是Server.c 中代码暂停的代码行。如果你没看到比如Client 程序,那你就要先上面方法启动Client 的调试。

这样你就很方便要这个运行到那,然后另一个运行到哪,然后观察交互,顺利找出各种问题。

介绍到此,望对你有用。 

猜你喜欢

转载自blog.csdn.net/leon_zeng0/article/details/107438624