为什么CGo不识别我在头文件中声明的结构?

时间:2016-09-12 18:35:04

标签: c go struct cgo

我收到错误" ./ test.h:10:3​​:错误:未知类型名称' PROCESS'" 当我加入my时头文件test.h,它将struct定义P​​ROCESS作为我的C Go Lang应用程序的一部分。代码在C中编译没有任何问题所以我想我做的事情非常简单......

package main

// #include <sys/mman.h>
// #include <errno.h>
// #include <inttypes.h>
// #include <stdlib.h>
// #include "test.h"
import "C"

import (
    "fmt"
    _"unsafe"
  )


func main() {
    fmt.Println("Retrieving process list");

}

test.h的内容如下......

#include <sys/mman.h>
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>

struct PROCESS {
    char *name;
    int os_type;
    addr_t address;
    PROCESS *next;

    //fields we care about
    unsigned int uid;
    unsigned int gid;
    unsigned int is_root;
    unsigned int io_r;
    unsigned int io_wr;
    unsigned int io_sys_r;
    unsigned int io_sys_wr;
    unsigned int used_super;
    unsigned int is_k_thread;
    unsigned int cpus;
    unsigned long hw_rss;
    unsigned long vma_size;
    unsigned long map_count;
    unsigned long pages;
    unsigned long total_map;
    unsigned long min_flt;
    unsigned long mm_usrs;
    unsigned long nr_ptes;
    unsigned long nvcsw;

};

1 个答案:

答案 0 :(得分:3)

在C中,(与C ++不同),struct关键字不声明可以单独使用的类型名称;它需要使用struct关键字进行限定。类型为struct PROCESS而不是PROCESS

struct PROCESS 
{
    char* name ;
    int os_type ;
    addr_t address ;
    struct PROCESS* next ;   // The struct keyword is needed here
    ...