go - 找不到带有编译指示包的结构的某些字段,64 位整数
问题描述
package main
/*
#include <inttypes.h>
#pragma pack(1)
typedef struct _sss {
uint64_t some; // ok
uint32_t wow; // ok
uint64_t some2; // not found
uint64_t some3; // not found
uint64_t some4; // not found
uint32_t some5; // ok
} Type;
#pragma pack()
*/
import "C"
import (
"fmt"
)
func main() {
s := C.Type{}
s.some = 10;
s.wow = 10;
s.some2 = 10;
s.some5 = 10;
fmt.Println("Hello, playground")
}
我声明了名为 C.Type 的结构。
如果 pragma pack(1) 和 64 位成员一起使用。
╭─dire@dire-81w4 ~/workspace
╰─go run test.go 2 ↵
# command-line-arguments
./test.go:25:6: s.some2 undefined (type _Ctype_struct__sss has no field or method some2)
通过删除 pragma pack(1) 可以进行编译。
正在使用 Go 1.15 版本。你知道为什么吗?
解决方案
我找到了关于我的问题的答案。
If the fields of a C struct are aligned such that they can't be represented by a Go struct, then they cannot be accessed directly from cgo. You will have to write functions in C to read and write those fields.
我现在太忙了,无法发表评论。来晚了,但已经回复了。
推荐阅读
- azure-devops - 无法删除 Azure Dev Ops 中的测试计划/套件
- amazon-web-services - AWS Cloudformation cfn-init 未安装软件包
- amazon-web-services - 如何在 AWS 上接收数据 MQTT
- bots - 我如何制作欢迎命令并添加选项来设置它将发送到的频道?
- javascript - 如何使用 tampermonkey 用户脚本 javascript 延迟网站上的页面加载
- ruby-on-rails - 构建方法不保存记录
- sql-server - SQL Server Express Edition 功能和升级问题
- python - 给定数的亚当数
- c# - 微调器适配器 - Xamarin Android
- mysql - MySQL 用一个使用 COUNT(*) 的查询选择多个字段