无法访问CGO中的C变量

I'm trying to access a c struct in cgo, but go this

could not determine kind of name for C.utmpx

utmpx is a c struct

here is the go code:

/*
#include <stdio.h>
#include <stdlib.h>
#include <utmpx.h>
#include <fcntl.h>
#include <unistd.h>
*/
import "C"

type record C.utmpx

fd, err := os.Open(C._PATH_UTMPX) // this works
fd, err := os.Open(C.UTMPX_FILE)  // error

In the utmpx.h file , there is

 #define    _PATH_UTMPX     "/var/run/utmpx"
 #define    UTMPX_FILE  _PATH_UTMPX

I can use _PATH_UTMPX but get the same warn when using UTMPX_FILE, why?

It seems that I cannot access these variables declared in .h file How can i do this ?

platform: macOS sirria,go 1.8

cgo c
duanfuxing2417
duanfuxing2417 真的很抱歉耽搁
接近 3 年之前 回复
dongpo2014
dongpo2014 问题解决了?
大约 3 年之前 回复

1个回答

#define's are problematic with CGo. I could get it to work with Go 1.8.1 on Linux amd64 like this:

package main

import "os"

/*
#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <utmpx.h>
#include <fcntl.h>
#include <unistd.h>

char *path_utmpx = UTMPX_FILE;

typedef struct utmpx utmpx;
*/
import "C"

type record C.utmpx

func main() {
    path := C.GoString(C.path_utmpx)
    fd, err := os.Open(path)
    if err != nil {
        panic("bad")
    }
    fd.Close()
}
  1. I had to define _GNU_SOURCE to get the UTMPX_FILE definition.
  2. I had to create the path_utmpx variable to get around the #define problems with CGo.
  3. I had to do the typedef to get type record C.utmpx compile.
  4. With Go, you can't use C strings directly. You must convert them to Go strings. Similarly, if you want to call C functions with Go strings, you must convert them to C strings (and free the space allocated in the heap).

A few pointers:

Good luck!

duan2428
duan2428 stackoverflow.com/q/45318603/5819540
接近 3 年之前 回复
dongzhanbi0027
dongzhanbi0027 当然。 在论坛上打开另一个问题。
接近 3 年之前 回复
drgaeqdqiiyg14608
drgaeqdqiiyg14608 我无法使用fd.Read()读取任何数据。 你能帮助我吗?
接近 3 年之前 回复
douchong4730
douchong4730 伟大的伟大答案。 它对我来说没有#define _GNU_SOURCE 1就可以工作。 go版本号是1.8,而os是macOS el capitan。
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问