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