How to debug Go with Cgo in Visual Studio Code

huangapple go评论95阅读模式
英文:

How to debug Go with Cgo in Visual Studio Code

问题

我可以在 Visual Studio Code 中设置断点并逐步执行 Go 代码,如果没有 Cgo 代码的话是没有问题的。一旦在 Go 代码中调用了 Cgo 代码,断点基本上会被忽略,尽管应用程序正常运行。

以下是代码片段的翻译:

//hello.c
#include <stdio.h>

void Test() {
  printf("C: Hello world");
}

//hello.go
package main

// #cgo LDFLAGS: -Wl,--allow-multiple-definition
// #cgo CFLAGS: -Wall -std=c99 -O1 -g
/*
#include "hello.c"
*/
import "C"

import "fmt"

func main() {
    // 调用无参数的 void 函数
    C.Test()
    fmt.Printf("Go: Hello world\n")
}

//launch.json
{
	// 使用 IntelliSense 了解可能的属性。
	// 悬停以查看现有属性的描述。
	// 获取更多信息,请访问:https://go.microsoft.com/fwlink/?linkid=830387
	"version": "0.2.0",
	"configurations": [
		{
			"name": "Launch Package",
			"type": "go",
			"request": "launch",
			"mode": "debug",
			"program": "learning"
		}
	]
}
英文:

I can set breakpoints and step through the Go code without issues in Visual Studio Code if there is no Cgo code. Once Cgo code is called in the Go code, breakpoints are basically ignored, although the application runs fine.

Here is the snippet:

//hello.c
#include &lt;stdio.h&gt;

void Test() {
  printf(&quot;C: Hello world&quot;);
}

//hello.go
package main

// #cgo LDFLAGS: -Wl,--allow-multiple-definition
// #cgo CFLAGS: -Wall -std=c99 -O1 -g
/*
#include &quot;hello.c&quot;
*/
import &quot;C&quot;

import &quot;fmt&quot;

func main() {
    //Call to void function without params
    C.Test()
    fmt.Printf(&quot;Go: Hello world\n&quot;)
}

//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
	&quot;version&quot;: &quot;0.2.0&quot;,
	&quot;configurations&quot;: [
		{
			&quot;name&quot;: &quot;Launch Package&quot;,
			&quot;type&quot;: &quot;go&quot;,
			&quot;request&quot;: &quot;launch&quot;,
			&quot;mode&quot;: &quot;debug&quot;,
			&quot;program&quot;: &quot;learning&quot;
		}
	]
}

答案1

得分: 2

这是目前已知的一个bug。这个问题可能会在go 1.18中修复。你通常可以在Linux上进行调试。

英文:

This is currently a known bug. This may get fixed in go 1.18. You can normally debug on Linux.

huangapple
  • 本文由 发表于 2021年11月10日 04:39:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/69904782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定