找不到带有编译指示包的结构的某些字段,64 位整数
Can't find struct's some field with pragma pack, 64 bits integer
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.
我现在太忙了,无暇发表评论。来晚了,还是回复了。
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.
我现在太忙了,无暇发表评论。来晚了,还是回复了。