How to debug Go with Cgo in Visual Studio Code

Viewed 489

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 Answers

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

Related