FAQ
This may have been answered before. I tried a few searches and didn't see
anything that helped me. I'm trying to learn how to interface my Go program
with C libraries and have contrived the following Go program:

package main

// #include <stdlib.h>
import "C"
import "unsafe"

func main() {
var s *C.char
s = C.malloc(C.size_t(unsafe.Sizeof(C.char) * 6))
C.free(s)
}

I'm compiling it like so:

$ go tool cgo main.go
$ cd _obj
$ go build main.cgo1.go

"go tool cgo" seems work fine and generates a bunch of files in the _obj
subdirectory. However, I'm getting the following output from "go build":

# command-line-arguments
main.go:8[/tmp/_obj/main.cgo1.go:13]: undefined: _Ctype_char
main.go:9[/tmp/_obj/main.cgo1.go:14]: undefined: _Cfunc_malloc
main.go:9[/tmp/_obj/main.cgo1.go:14]: undefined: _Ctype_size_t
main.go:9[/tmp/_obj/main.cgo1.go:14]: undefined: _Ctype_char
main.go:9[/tmp/_obj/main.cgo1.go:14]: invalid expression
unsafe.Sizeof(_Ctype_char)
main.go:9[/tmp/_obj/main.cgo1.go:16]: undefined: _Cfunc_free

I'm using Go 1.0.2. Any thoughts about what I'm missing? Thanks.

Kamil

--

Search Discussions

  • Minux at Sep 21, 2012 at 5:48 am

    On Fri, Sep 21, 2012 at 8:15 AM, wrote:

    This may have been answered before. I tried a few searches and didn't see
    anything that helped me. I'm trying to learn how to interface my Go program
    with C libraries and have contrived the following Go program:

    package main

    // #include <stdlib.h>
    import "C"
    import "unsafe"

    func main() {
    var s *C.char
    s = C.malloc(C.size_t(unsafe.Sizeof(C.char) * 6))
    C.free(s)
    }

    I'm compiling it like so:

    $ go tool cgo main.go
    $ cd _obj
    $ go build main.cgo1.go
    Have you read these two articles about cgo?
    http://golang.org/doc/articles/c_go_cgo.html
    http://golang.org/cmd/cgo/

    --
  • Raph at Sep 21, 2012 at 7:41 am
    read the articles minux posted. I don't think you need to call "go tool
    cgo" directly at all. I use go build when building go code that interops
    with c.
    On Friday, September 21, 2012 1:15:24 AM UTC+1, Kamil Jiwa wrote:

    This may have been answered before. I tried a few searches and didn't see
    anything that helped me. I'm trying to learn how to interface my Go program
    with C libraries and have contrived the following Go program:

    package main

    // #include <stdlib.h>
    import "C"
    import "unsafe"

    func main() {
    var s *C.char
    s = C.malloc(C.size_t(unsafe.Sizeof(C.char) * 6))
    C.free(s)
    }

    I'm compiling it like so:

    $ go tool cgo main.go
    $ cd _obj
    $ go build main.cgo1.go

    "go tool cgo" seems work fine and generates a bunch of files in the _obj
    subdirectory. However, I'm getting the following output from "go build":

    # command-line-arguments
    main.go:8[/tmp/_obj/main.cgo1.go:13]: undefined: _Ctype_char
    main.go:9[/tmp/_obj/main.cgo1.go:14]: undefined: _Cfunc_malloc
    main.go:9[/tmp/_obj/main.cgo1.go:14]: undefined: _Ctype_size_t
    main.go:9[/tmp/_obj/main.cgo1.go:14]: undefined: _Ctype_char
    main.go:9[/tmp/_obj/main.cgo1.go:14]: invalid expression
    unsafe.Sizeof(_Ctype_char)
    main.go:9[/tmp/_obj/main.cgo1.go:16]: undefined: _Cfunc_free

    I'm using Go 1.0.2. Any thoughts about what I'm missing? Thanks.

    Kamil
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 21, '12 at 4:49a
activeSep 21, '12 at 7:41a
posts3
users3
websitegolang.org

3 users in discussion

Minux: 1 post Raph: 1 post Kamil Jiwa: 1 post

People

Translate

site design / logo © 2022 Grokbase