dongzhang6544 2019-01-05 03:22
浏览 416
已采纳

在Golang包中编译CGO文件

I am trying to use CGO to bundle C files with a Golang package. Following instructions here:

https://karthikkaranth.me/blog/calling-c-code-from-go/

http://akrennmair.github.io/golang-cgo-slides/#1

https://golang.org/cmd/cgo/

I am getting this error:

# 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

here is the structure:

enter image description here

main.go just looks like:

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))
}

and I run test.sh to build it:

#!/usr/bin/env bash

dir="$(cd `dirname "$0"` && pwd)"
export GOPATH="$dir"

cd "$dir"

export CGOFILES=main
go install main

but when I run the bash script I get that error.

  • 写回答

2条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 blender python输出纹理坐标(像素级)与三维坐标的对应关系
      • ¥15 python文件分发
      • ¥15 关于#c语言#的问题:要求:输出格式要界面直观、清晰大方、格式规范
      • ¥15 计算机组成汇编语言电路升级
      • ¥15 如何使用代理服务器没法改回来了?
      • ¥15 hadoop网站点击流数据分析项目
      • ¥15 TDA4 USB 识别不到的问题
      • ¥36 禁用ssl重协商以达到解决DOS漏洞
      • ¥20 cad 编写网表完成以下问题
      • ¥15 materials studio软件的forcite模块