在Golang包中编译CGO文件

时间:2019-01-05 03:22:05

标签: go cgo goinstall

我正在尝试使用CGO将C文件与Golang软件包捆绑在一起。请按照以下说明进行操作:

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

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

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

我收到此错误:

# 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

这是结构:

enter image description here

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脚本时,我得到了这个错误。

2 个答案:

答案 0 :(得分:2)

我按照说明进行操作:

  

Command cgo

     

如果“ C”的导入是立即,并带有注释,则表示   注释,称为前导,在编译C时用作标题   包装的各个部分。例如:

// #include <stdio.h>
// #include <errno.h>
import "C"
     

/*
#include <stdio.h>
#include <errno.h>
*/
import "C"

例如,

gocbuf.go

package main

import (
    "fmt"
    "unsafe"
)

/*
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int printData(unsigned char *data) {
    return printf("cData: %lu \"%s\"\n", (long unsigned int)strlen(data), data);
}
*/
import "C"

func main() {
    // Allocate C data buffer.
    width, height := 8, 2
    lenData := width * height
    // add string terminating null byte
    cData := (*C.uchar)(C.calloc(C.size_t(lenData+1), C.sizeof_uchar))

    // When no longer in use, free C allocations.
    defer C.free(unsafe.Pointer(cData))

    // Go slice reference to C data buffer,
    // minus string terminating null byte
    gData := (*[1 << 30]byte)(unsafe.Pointer(cData))[:lenData:lenData]

    // Write and read cData via gData.
    for i := range gData {
        gData[i] = '.'
    }
    copy(gData[0:], "Data")
    gData[len(gData)-1] = 'X'
    fmt.Printf("gData: %d %q\n", len(gData), gData)
    C.printData(cData)
}

输出:

$ go run gocbuf.go
gData: 16 "Data...........X"
cData: 16 "Data...........X"
$ 

您的代码组织对我来说毫无意义。

您应该拥有软件包greeter,该软件包通过C包装了cgo函数。例如,

src
└── greeter
    ├── greeter.c
    ├── greeter.go
    └── greeter.h

带有骨架文件

greeter.go

package greeter

/*
#include "greeter.h"
*/
import "C"

greeter.c

#include "greeter.h"

greeter.h

/* C header file */

要安装greeter软件包,只需使用go install

不要使用相对路径。不要使用bash脚本。

答案 1 :(得分:0)

感谢@peterSO,这是行得通的:

package main

// #cgo CFLAGS: -g -Wall
// #include <stdlib.h>
// #include "../genericc/greeter.h"
// #include "../genericc/greeter.c"    // ! no whitespace after this line
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))
}