FAQ
I'm trying to call a C function from a library that requires the
declaration of a global C string (char*) so I tried:

/*
char *process_name;
*/

import "C"

import "foo"

C.process_name = C.CString("foo")

when I build I get the error:

/tmp/go-build422946716/foo/_obj/rlog.cgo2.o: In function `wrap_rlog':
../../../../go/src/foo/rlog.go:10: undefined reference to `process_name'
collect2: ld returned 1 exit status

Is there a way to do this if so how?

Ted

--
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/d/optout.

Search Discussions

  • Rogerjd at Sep 11, 2015 at 11:35 pm
    This may help:
    Cgo lets Go packages call C code.

    Roger
    On Friday, September 11, 2015 at 6:55:50 PM UTC-4, Ted Toth wrote:

    I'm trying to call a C function from a library that requires the
    declaration of a global C string (char*) so I tried:

    /*
    char *process_name;
    */

    import "C"

    import "foo"

    C.process_name = C.CString("foo")

    when I build I get the error:

    /tmp/go-build422946716/foo/_obj/rlog.cgo2.o: In function `wrap_rlog':
    ../../../../go/src/foo/rlog.go:10: undefined reference to `process_name'
    collect2: ld returned 1 exit status

    Is there a way to do this if so how?

    Ted
    --
    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/d/optout.
  • Guillaume J. Charmes at Sep 12, 2015 at 12:18 am
    You can't affect values in the global scope in Go, you can only declare
    variable/functions.

    In order to achieve what you want you can use the init() function.

    example (leaky):

    package main

    // char *process_name;
    import "C"
    import "fmt"

    func init() {
             C.process_name = C.CString("foo")
    }

    func main() {
             fmt.Println(C.GoString(C.process_name))
    }

    --

    Guillaume J. Charmes
    about.me/guillaumecharmes
    [image: Guillaume J. Charmes on about.me]
       <http://about.me/guillaumecharmes>
    On Fri, Sep 11, 2015 at 6:35 PM, rogerjd wrote:

    This may help:
    Cgo lets Go packages call C code.

    Roger
    On Friday, September 11, 2015 at 6:55:50 PM UTC-4, Ted Toth wrote:

    I'm trying to call a C function from a library that requires the
    declaration of a global C string (char*) so I tried:

    /*
    char *process_name;
    */

    import "C"

    import "foo"

    C.process_name = C.CString("foo")

    when I build I get the error:

    /tmp/go-build422946716/foo/_obj/rlog.cgo2.o: In function `wrap_rlog':
    ../../../../go/src/foo/rlog.go:10: undefined reference to `process_name'
    collect2: ld returned 1 exit status

    Is there a way to do this if so how?

    Ted

    --
    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/d/optout.
    --
    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/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 11, '15 at 10:55p
activeSep 12, '15 at 12:18a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase