【问题标题】:debugging cgo in visual studio code produces "multiple definition of" error在 Visual Studio 代码中调试 cgo 会产生“多重定义”错误
【发布时间】:2020-10-14 01:03:05
【问题描述】:

我正在尝试在 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

//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 代码中调试此应用程序,或者是否有其他方法可以调试?

【问题讨论】:

    标签: go visual-studio-code cgo


    【解决方案1】:

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

    我已经在linux上转载了。

    你能试试go build -gcflags="all=-N -l" ./

    //#cgo LDFLAGS: -Wl,--allow-multiple-definition 
    import "C" 
    

    golang/go#16513 中所述?

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 2015-08-04
      • 2021-12-22
      相关资源
      最近更新 更多