3

我使用以下 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
 );
4

1 回答 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包在不兼容的指针类型之间进行转换)。

于 2013-10-02T00:40:22.540 回答