英文:
GoLang / CGO: Problems calling Mach API host_statistics() from Go
问题
我使用以下的C代码片段来获取OS X上的CPU负载:
#include <mach/message.h>
#include <mach/mach_host.h>
#include <mach/host_info.h>
[...]
mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
kern_return_t error;
host_cpu_load_info_data_t r_load;
mach_port_t host_port = mach_host_self();
error = host_statistics(host_port, HOST_CPU_LOAD_INFO, (host_info_t)&r_load, &count);
我尝试在阅读了cgo教程后将这段代码移植到Go语言中,结果的代码如下:
package main
/*
#include <stdlib.h>
#include <mach/message.h>
#include <mach/mach_host.h>
#include <mach/host_info.h>
*/
import "C"
func main() {
var err C.kern_return_t
var host_info_out C.host_info_t
var host_port C.mach_port_t = C.mach_host_self()
count := C.mach_msg_type_number_t(C.HOST_CPU_LOAD_INFO_COUNT)
err = C.host_statistics(C.host_t(host_port), C.HOST_CPU_LOAD_INFO, &host_info_out, &count)
}
然而,当我尝试构建这段代码时,我得到了以下错误信息:
go build cputimes.go
# command-line-arguments
cputimes.go:33: cannot use &host_info_out (type *_Ctype_host_info_t) as type *_Ctype_integer_t in function argument
我不明白为什么cgo会抱怨类型。host_statistics()的签名在mach头文件中定义如下:
kern_return_t host_statistics
(
host_t host_priv,
host_flavor_t flavor,
host_info_t host_info_out,
mach_msg_type_number_t *host_info_outCnt
);
英文:
I use the following C code fragment to get the CPU load on OS X:
#include <mach/message.h>
#include <mach/mach_host.h>
#include <mach/host_info.h>
[...]
mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
kern_return_t error;
host_cpu_load_info_data_t r_load;
mach_port_t host_port = mach_host_self();
error = host_statistics(host_port, HOST_CPU_LOAD_INFO, (host_info_t)&r_load, &count);
I have tried to port this piece of code to Go after reading the cgo tutorial. The resulting code looks like this:
package main
/*
#include <stdlib.h>
#include <mach/message.h>
#include <mach/mach_host.h>
#include <mach/host_info.h>
*/
import "C"
func main() {
var err C.kern_return_t
var host_info_out C.host_info_t
var host_port C.mach_port_t = C.mach_host_self()
count := C.mach_msg_type_number_t(C.HOST_CPU_LOAD_INFO_COUNT)
err = C.host_statistics(C.host_t(host_port), C.HOST_CPU_LOAD_INFO, &host_info_out, &count)
}
However, when I try to build the code, I end up with the following error message
go build cputimes.go
# command-line-arguments
cputimes.go:33: cannot use &host_info_out (type *_Ctype_host_info_t) as type *_Ctype_integer_t in function argument
I do not udnerstand why cgo complains about the type. The signature of host_statistics() is defined in the mach headers as:
kern_return_t host_statistics
(
host_t host_priv,
host_flavor_t flavor,
host_info_t host_info_out,
mach_msg_type_number_t *host_info_outCnt
);
答案1
得分: 1
函数原型表明host_statistics
的第三个参数是一个host_info_t
变量,而你在示例程序中传递的是一个指向host_info_t
变量的指针。
查看mach/host_info.h
头文件,我们可以看到host_info_t
是一个指针类型:
typedef integer_t *host_info_t; /* varying array of int. */
这解释了为什么你会收到关于integer_t
类型不匹配的错误消息。
你的Go代码在处理该参数时似乎与C代码不太相等。你可能想要像这样做一些修改:
...
var r_load C.host_cpu_load_info_data_t
...
err = C.host_statistics(C.host_t(host_port), C.HOST_CPU_LOAD_INFO, C.host_info_t(unsafe.Pointer(&r_load)), &count)
...
(你需要使用unsafe
包在不兼容的指针类型之间进行转换)。
英文:
The function prototype says that the third argument to host_statistics
is a host_info_t
variable, while you are passing a pointer to a host_info_t
variable in your sample program.
Looking at the mach/host_info.h
header file, we can see that host_info_t
is a pointer type:
typedef integer_t *host_info_t; /* varying array of int. */
That explains why you got the error message about a type mismatch with integer_t
.
Your Go code doesn't really look equivalent to the C code when handling that argument. You probably want something more like this:
...
var r_load C.host_cpu_load_info_data_t
...
err = C.host_statistics(C.host_t(host_port), C.HOST_CPU_LOAD_INFO, C.host_info_t(unsafe.Pointer(&r_load)), &count)
...
(you need to use the unsafe
package to cast between the incompatible pointer types).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论