FAQ
Hi, I am trying to use the zlib library in Go using Cgo (yes, I know there
is a zlib library in Go, but I needed some advanced functionality only the
C version provides), but I was unable to compile my test program. When I
try to compile I get the following error:

# command-line-arguments
1: error: 'inflateInit' undeclared (first use in this function)
1: note: each undeclared identifier is reported only once for each function
it appears in


On the interwebs, it seems the most common issue with undeclared functions
is due to incorrectly included headers, but I think I have done so
correctly:

// #cgo LDFLAGS: -lz
// #include "zlib.h"
import "C"


Does anyone know why the following block fails to compile?

var strm C.z_stream
strm.zalloc = C.alloc_func(unsafe.Pointer(uintptr(C.Z_NULL)))
strm.zfree = C.free_func(unsafe.Pointer(uintptr(C.Z_NULL)))
strm.opaque = C.voidpf(uintptr(C.Z_NULL))
strm.avail_in = 0
strm.next_in = (*C.Bytef)(unsafe.Pointer(uintptr(C.Z_NULL)))
zerr(C.inflateInit(&strm)) // Fails here due to inflateInit not being
declared.
defer C.inflateEnd(&strm)


I'm fairly sure the zlib.h header is being included. If I remove the
inflateInit call, it compiles properly even though there are references to
other functions and structs in zlib.h like inflateEnd and z_stream. I
should note that if I create a C source wrapper around all the functions I
use, and then use the wrapper functions in Cgo, it magically works.
Is this a bug in cgo?

Thanks.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Ian Lance Taylor at Sep 24, 2013 at 7:36 pm

    On Tue, Sep 24, 2013 at 12:16 PM, wrote:
    Hi, I am trying to use the zlib library in Go using Cgo (yes, I know there
    is a zlib library in Go, but I needed some advanced functionality only the C
    version provides), but I was unable to compile my test program. When I try
    to compile I get the following error:

    # command-line-arguments
    1: error: 'inflateInit' undeclared (first use in this function)
    1: note: each undeclared identifier is reported only once for each function
    it appears in


    On the interwebs, it seems the most common issue with undeclared functions
    is due to incorrectly included headers, but I think I have done so
    correctly:

    // #cgo LDFLAGS: -lz
    // #include "zlib.h"
    import "C"


    Does anyone know why the following block fails to compile?

    var strm C.z_stream
    strm.zalloc = C.alloc_func(unsafe.Pointer(uintptr(C.Z_NULL)))
    strm.zfree = C.free_func(unsafe.Pointer(uintptr(C.Z_NULL)))
    strm.opaque = C.voidpf(uintptr(C.Z_NULL))
    strm.avail_in = 0
    strm.next_in = (*C.Bytef)(unsafe.Pointer(uintptr(C.Z_NULL)))
    zerr(C.inflateInit(&strm)) // Fails here due to inflateInit not being
    declared.
    defer C.inflateEnd(&strm)


    I'm fairly sure the zlib.h header is being included. If I remove the
    inflateInit call, it compiles properly even though there are references to
    other functions and structs in zlib.h like inflateEnd and z_stream. I should
    note that if I create a C source wrapper around all the functions I use, and
    then use the wrapper functions in Cgo, it magically works.
    Is this a bug in cgo?
    At least in the zlib.h on my system, inflateInit is a macro.
    Unfortunately cgo does not support calling macros directly. You have
    to use a C wrapper, as you tried.

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Thebrokentoaster at Sep 24, 2013 at 8:00 pm
    Ah, I see. That makes sense.

    Thanks.
    On Tuesday, September 24, 2013 12:36:22 PM UTC-7, Ian Lance Taylor wrote:

    On Tue, Sep 24, 2013 at 12:16 PM, <thebroke...@gmail.com <javascript:>>
    wrote:
    Hi, I am trying to use the zlib library in Go using Cgo (yes, I know there
    is a zlib library in Go, but I needed some advanced functionality only the C
    version provides), but I was unable to compile my test program. When I try
    to compile I get the following error:

    # command-line-arguments
    1: error: 'inflateInit' undeclared (first use in this function)
    1: note: each undeclared identifier is reported only once for each function
    it appears in


    On the interwebs, it seems the most common issue with undeclared functions
    is due to incorrectly included headers, but I think I have done so
    correctly:

    // #cgo LDFLAGS: -lz
    // #include "zlib.h"
    import "C"


    Does anyone know why the following block fails to compile?

    var strm C.z_stream
    strm.zalloc = C.alloc_func(unsafe.Pointer(uintptr(C.Z_NULL)))
    strm.zfree = C.free_func(unsafe.Pointer(uintptr(C.Z_NULL)))
    strm.opaque = C.voidpf(uintptr(C.Z_NULL))
    strm.avail_in = 0
    strm.next_in = (*C.Bytef)(unsafe.Pointer(uintptr(C.Z_NULL)))
    zerr(C.inflateInit(&strm)) // Fails here due to inflateInit not being
    declared.
    defer C.inflateEnd(&strm)


    I'm fairly sure the zlib.h header is being included. If I remove the
    inflateInit call, it compiles properly even though there are references to
    other functions and structs in zlib.h like inflateEnd and z_stream. I should
    note that if I create a C source wrapper around all the functions I use, and
    then use the wrapper functions in Cgo, it magically works.
    Is this a bug in cgo?
    At least in the zlib.h on my system, inflateInit is a macro.
    Unfortunately cgo does not support calling macros directly. You have
    to use a C wrapper, as you tried.

    Ian
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Mat at Nov 2, 2013 at 5:58 pm
    Did you get this done? I'm doing the same thing and wonder if it's worth
    getting an open source czlib package going?

    On Tuesday, September 24, 2013 1:16:21 PM UTC-6, thebroke...@gmail.com
    wrote:
    Hi, I am trying to use the zlib library in Go using Cgo (yes, I know there
    is a zlib library in Go, but I needed some advanced functionality only the
    C version provides), but I was unable to compile my test program. When I
    try to compile I get the following error:

    # command-line-arguments
    1: error: 'inflateInit' undeclared (first use in this function)
    1: note: each undeclared identifier is reported only once for each
    function it appears in


    On the interwebs, it seems the most common issue with undeclared functions
    is due to incorrectly included headers, but I think I have done so
    correctly:

    // #cgo LDFLAGS: -lz
    // #include "zlib.h"
    import "C"


    Does anyone know why the following block fails to compile?

    var strm C.z_stream
    strm.zalloc = C.alloc_func(unsafe.Pointer(uintptr(C.Z_NULL)))
    strm.zfree = C.free_func(unsafe.Pointer(uintptr(C.Z_NULL)))
    strm.opaque = C.voidpf(uintptr(C.Z_NULL))
    strm.avail_in = 0
    strm.next_in = (*C.Bytef)(unsafe.Pointer(uintptr(C.Z_NULL)))
    zerr(C.inflateInit(&strm)) // Fails here due to inflateInit not being
    declared.
    defer C.inflateEnd(&strm)


    I'm fairly sure the zlib.h header is being included. If I remove the
    inflateInit call, it compiles properly even though there are references to
    other functions and structs in zlib.h like inflateEnd and z_stream. I
    should note that if I create a C source wrapper around all the functions I
    use, and then use the wrapper functions in Cgo, it magically works.
    Is this a bug in cgo?

    Thanks.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 24, '13 at 7:16p
activeNov 2, '13 at 5:58p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase