dimlee dimlee - 1 year ago 99
C Question

Why doesn't CGo recognize my struct declared in a header file?

I am getting the error "./test.h:10:3: error: unknown type name 'PROCESS'" when I include my header file test.h that has the struct definition PROCESS as a part of my C Go Lang application. The code compiles in C with no problems so I imagine I'm doing something very simple incorrectly...

package main

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

import (

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


The contents of test.h are below...

#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;


Answer Source

In C, (unlike C++), the struct keyword does not declare a type-name that can be used on its own; it needs qualification with the struct keyword. The The type is struct PROCESS not PROCESS:

struct PROCESS 
    char* name ;
    int os_type ;
    addr_t address ;
    struct PROCESS* next ;   // The struct keyword is needed here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download