英文:
cgo: use typedef struct in preamble
问题
我正在尝试为lirc创建Go绑定:https://github.com/inando/go-lirc
像lirc_init()和lirc_deinit()这样的简单函数工作正常。
对于函数'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;
我首先尝试了类似这样的代码:
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
}
但是这给我报了一个错误:could not determine kind of name for C.lirc_command_init。
不确定是否应该为类型使用struct_?
ctx可能需要是一个指针,但我总是得到相同的错误。
然后我尝试使用一个包装器,但这给我报了一个错误:unknown type name '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"
我在这里做错了什么?我如何在Go中使用那个结构体类型?
更新:
不确定是否与此有关,但C.free也报错。
p := C.CString(prog)
defer C.free(unsafe.Pointer(p))
-> could not determine kind of name for C.free
Go版本:
go version go1.4 linux/amd64 (Vagrant on windows)
英文:
I'm trying to make Go bindings for lirc:
https://github.com/inando/go-lirc
Simple functions like lirc_init() and lirc_deinit() work fine.
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
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.
Not sure if the struct_ should be used for a type?
The ctx probably needs to be a pointer, but I always get the same error.
Then I tried with a wrapper, but this is giving me the error unknown type name '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?
Update:
Not sure if this is related, but C.free also complains.
p := C.CString(prog)
defer C.free(unsafe.Pointer(p))
-> could not determine kind of name for C.free
Go version:
go version go1.4 linux/amd64 (Vagrant on windows)
答案1
得分: 5
could not determine kind of name for ...
消息是在某个东西未定义时出现的。对于C.free
,请将stdlib.h
添加到您的包含文件中。
#include <stdlib.h>
其他错误也是类似的情况,只是导入了错误的头文件或错误版本的头文件。在一个随机的Ubuntu系统上检查,lirc_client.h
文件与您提供的链接中的文件非常不同。确保您使用了正确的版本,其中定义了类型。
英文:
The could not determine kind of name for ...
message is what you get when something isn't defined. In the case of C.free
, add stdlib.h
to your includes.
#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. Make sure you're using the correct version where the types are defined.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论