为什么 CGo 不能识别我在头文件中声明的结构?
Why doesn't CGo recognize my struct declared in a header file?
我收到错误 "./test.h:10:3: error: unknown type name 'PROCESS'" 当我包含我的头文件时test.h 具有结构定义 PROCESS 作为我的 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;
};
在 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
...
我收到错误 "./test.h:10:3: error: unknown type name 'PROCESS'" 当我包含我的头文件时test.h 具有结构定义 PROCESS 作为我的 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;
};
在 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
...