Cannot access c variables in cgo

前端 未结 1 595
闹比i
闹比i 2021-01-26 02:20

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

相关标签:
1条回答
  • 2021-01-26 03:06

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

    • https://blog.golang.org/c-go-cgo
    • https://golang.org/cmd/cgo/

    Good luck!

    0 讨论(0)
提交回复
热议问题