登录
首页 >  Golang >  Go问答

在 Visual Studio Code 中如何使用 Cgo 为 Go 代码调试

来源:stackoverflow

时间:2024-02-27 11:06:19 479浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《在 Visual Studio Code 中如何使用 Cgo 为 Go 代码调试》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

如果没有 cgo 代码,我可以在 visual studio code 中设置断点并单步执行 go 代码,而不会出现问题。一旦go代码中调用了cgo代码,断点基本上会被忽略,尽管应用程序运行良好。

这是片段:

//hello.c
#include 

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"
        }
    ]
}

正确答案


这是目前已知的 bug。这可能会在 go 1.18 中得到修复。在Linux上可以正常调试。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《在 Visual Studio Code 中如何使用 Cgo 为 Go 代码调试》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>