英文:
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 <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() {
//Call to void function without params
C.Test()
fmt.Printf("Go: Hello world\n")
}
//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": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "learning"
}
]
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论