有以下一个 C 的结构体
#pragma pack(1) //单字节对齐
typedef struct{
unsigned char a[2048];
unsigned int b;
}sampleStruct;
#pragma pack()
在 Golang 中给这个结构体赋值
var sampleStruct C.sampleStruct = C.sampleStruct{
[2048]C.uchar{}
C.uint(0)
}
理论上应该是成功的,但是编译器却提示我
cannot use _Ctype_uint(0) (type _Ctype_uint) as type [4]byte in field value
测试平台
有大佬知道为什么会这样吗
另外,定义如下函数
unsigned int add(unsigned int a, unsigned int b) {
return a + b;
}
在 golang 中使用如下调用
C.add(C.uint(2), C.uint(1))
是成功的
1
eloah OP 额......我给的这个去除了业务逻辑的代码是可以运行的......除了小的语法错误......
我再看看...... |
2
eloah OP 因为 Golang 和 C 的对齐方式差异,可能出现这类问题
https://github.com/Shopify/sysv_mq/issues/10 |