【发布时间】: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