我正在尝试使用 CGO 将 C 文件与 Golang 包捆绑在一起。遵循此处的说明:
https://karthikkaranth.me/blog/calling-c-code-from-go/
http://akrennmair.github.io/golang-cgo-slides/#1
我收到此错误:
# main src/main/main.go:16:8: could not determine kind of name for C.free src/main/main.go:23:10: could not determine kind of name for C.greet
这是结构:
main.go
看起来像:
package main
// #cgo CFLAGS: -g -Wall
// #include <stdlib.h>
// #include "genericc/greeter.h"
import "C"
import (
"fmt"
"unsafe"
)
func main() {
name := C.CString("Gopher")
defer C.free(unsafe.Pointer(name))
year := C.int(2018)
ptr := C.malloc(C.sizeof_char * 1024)
defer C.free(unsafe.Pointer(ptr))
size := C.greet(name, year, (*C.char)(ptr))
b := C.GoBytes(ptr, size)
fmt.Println(string(b))
}
我运行 test.sh 来构建它:
#!/usr/bin/env bash
dir="$(cd `dirname "$0"` && pwd)"
export GOPATH="$dir"
cd "$dir"
export CGOFILES=main
go install main
但是当我运行 bash 脚本时,我得到了那个错误。