简体   繁体   English

cgo:在序言中使用typedef结构

[英]cgo: use typedef struct in preamble

I'm trying to make Go bindings for lirc: https://github.com/inando/go-lirc 我正在尝试为lirc制作Go绑定: https : //github.com/inando/go-lirc

Simple functions like lirc_init() and lirc_deinit() work fine. 像lirc_init()和lirc_deinit()这样的简单函数可以正常工作。

For the function 'lirc_command_init()' I need to use a struct type: https://github.com/inando/lirc/blob/master/lib/lirc_client.h#L334 对于功能'lirc_command_init()',我需要使用一种结构类型: https : //github.com/inando/lirc/blob/master/lib/lirc_client.h#L334

typedef struct {
    char packet[PACKET_SIZE + 1];
    char buffer[PACKET_SIZE + 1];
    char reply[PACKET_SIZE + 1]; 
    int head;
    int reply_to_stdout;
    char* next;
} lirc_cmd_ctx;

I first tried something like this: 我首先尝试了这样的事情:

func lircCommandInit(format string, v ...interface{}) (todoctx string, err error) {
    var ctx C.struct_lirc_cmd_ctx
    cmd := C.CString(fmt.Sprintf(format, v...))
    ok, err := C.lirc_command_init(ctx, cmd)
    fmt.Println(ok, err)
    return
}

But this gives me this error: could not determine kind of name for C.lirc_command_init. 但这给了我这个错误:无法确定C.lirc_command_init的名称种类。
Not sure if the struct_ should be used for a type? 不确定struct_是否应用于类型?
The ctx probably needs to be a pointer, but I always get the same error. ctx可能需要是一个指针,但是我总是遇到相同的错误。

Then I tried with a wrapper, but this is giving me the error unknown type name 'lirc_cmd_ctx' 然后我尝试使用包装器,但这给了我一个错误,错误类型名称为'lirc_cmd_ctx'

// #cgo LDFLAGS: -llirc_client
// #cgo CFLAGS: -I /usr/include/lirc
// #include <lirc_client.h>
//
// int lirc_command_init_custom(const char* msg)
// {
//     lirc_cmd_ctx ctx;
//     return -2;
// }
import "C"

What am I doing wrong here? 我在这里做错了什么? How can I use that struct type in Go? 如何在Go中使用该结构类型?

Update: 更新:
Not sure if this is related, but C.free also complains. 不知道这是否相关,但是C.free也抱怨。

p := C.CString(prog)
defer C.free(unsafe.Pointer(p))

-> could not determine kind of name for C.free ->无法确定C.free的名称种类

Go version: go version go1.4 linux/amd64 (Vagrant on windows) 转到版本:转到版本go1.4 linux / amd64(Windows上为Vagrant)

The could not determine kind of name for ... message is what you get when something isn't defined. 如果未定义某些内容,则无法确定could not determine kind of name for ...消息could not determine kind of name for ... In the case of C.free , add stdlib.h to your includes. 对于C.free ,将stdlib.h添加到您的include中。

#include <stdlib.h>

The same goes for the other errors, except this is a case of importing the incorrect header, or the incorrect version of that header. 其他错误也是如此,只是这种情况是导入不正确的标头或该标头的错误版本的情况。 Checking on a random ubuntu system, the lirc_client.h file is very different from the one you linked. 在随机ubuntu系统上检查时, lirc_client.h文件与您链接的文件有很大不同。 Make sure you're using the correct version where the types are defined. 确保您使用的是定义了类型的正确版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM