go - 在Visual Studio代码中调试cgo会产生“多个定义”错误

标签 go visual-studio-code cgo

我正在尝试在Visual Studio代码中调试一个非常简单的cgo应用程序,但出现错误。当我go run main.go时,应用程序运行正常,没有问题。但是,当我尝试在Visual Studio代码中使用调试器时,出现以下错误:

# prolink007/simple-cgo
C:\Users\proli\AppData\Local\Temp\go-build964876546\b001\_x003.o: In function `Hello':
./hello.c:4: multiple definition of `Hello'
C:\Users\proli\AppData\Local\Temp\go-build964876546\b001\_x002.o:d:/projects/simple-cgo/main.go:6: first defined here
collect2.exe: error: ld returned 1 exit status
exit status 2
Process exiting with code: 1
这是我的main.go
package main

/*
  #include "hello.c"
*/
import "C"
import (
    "errors"
    "log"
)

func main() {
    err := HelloWorld()
    if err != nil {
        log.Fatal(err)
    }
}

func HelloWorld() error {
    _, err := C.Hello()
    if err != nil {
        return errors.New("error calling Hello function: " + err.Error())
    }

    return nil
}
这是我的问候
//hello.c
#include <stdio.h>

void Hello(){
    printf("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",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${fileDirname}",
            "env": {},
            "args": []
        }
    ]
}
我该如何在Visual Studio代码中调试此应用程序,或者有其他方法可以调试此应用程序?

最佳答案

我在github上问了同样的问题,得到了解决我问题的答案。

我已经在Linux上复制了它。
你可以尝试用-b建立build -gcflags =“all = -N -l” ./
//#cgo LDFLAGS:-Wl,-allow-multiple-definition import“C” as
在golang / go#16513中描述了吗?

关于go - 在Visual Studio代码中调试cgo会产生“多个定义”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62543721/

相关文章:

http - 从外部 Go 中间件控制 HTTP header

go - 如何将元数据添加到 Stripe 中的新订阅?

typescript - Vue/Typescript,获取模块 '"*.vue"' 没有导出成员

c# - 使用 HTTP 触发器读取和写入 Blob 的 Azure 函数

智能感知快速修复

go - 在 Golang 项目中正确包含 C 库(按源代码)

go模块没有正确下载动态库的cgo软链接(soft link)

c - 如何使用 cgo 用 argv 包装 C "hello world"?

go - 将 CIDR block 转换为 net.IP 类型

go - 如果在 gocql 中不存在,如何插入