FAQ
I want to use c code from go. I do the following

in src/num

I make a c file num.c:

int getNum()
{
     return 5;
}

with the header file num.h:

int getNum();

and go file num.go:

package num

/*
#cgo LDFLAGS: num.o
#include "num.h"
*/
import "C"

import "fmt"

func GetNum() int {
     var n int = int(C.getNum())
     fmt.Printf("number: %d", n)
     return n;
}

I then compile the c file:
gcc -c num.c build go:
go build num.go
Everything seems fine. But when i try to install it, this happens:
go install num
num.o: In function `getNum':
num.c:(.text+0x0): multiple definition of `getNum'
/tmp/go-build564793176/loadtool/lcrypt/_obj/num.o:/home/anders/go/src/num/num.c:5:
first defined here
collect2: error: ld returned 1 exit status


I can not fine any examples online where c code from the same directory is
included in go. Is this not possible or am I doing something wrong?

--
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 Oct 21, 2013 at 5:17 pm

    On Mon, Oct 21, 2013 at 7:00 AM, wrote:
    I want to use c code from go. I do the following

    in src/num

    I make a c file num.c:

    int getNum()
    {
    return 5;
    }

    with the header file num.h:

    int getNum();

    and go file num.go:

    package num

    /*
    #cgo LDFLAGS: num.o
    #include "num.h"
    */
    import "C"

    import "fmt"

    func GetNum() int {
    var n int = int(C.getNum())
    fmt.Printf("number: %d", n)
    return n;
    }

    I then compile the c file:
    gcc -c num.c build go:
    go build num.go
    Everything seems fine. But when i try to install it, this happens:
    go install num
    num.o: In function `getNum':
    num.c:(.text+0x0): multiple definition of `getNum'
    /tmp/go-build564793176/loadtool/lcrypt/_obj/num.o:/home/anders/go/src/num/num.c:5:
    first defined here
    collect2: error: ld returned 1 exit status


    I can not fine any examples online where c code from the same directory is
    included in go. Is this not possible or am I doing something wrong?

    When go build sees .c files in the package directory, it will compile
    them and link against them. So when you put num.o in LDFLAGS, the
    effect is that num.o is linked into the binary twice. Thus the
    multiple definition error.

    It should work fine if you omit the LDFLAGS line and don't bother to
    compile num.c yourself.

    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.
  • Three Eyed Pike at Oct 22, 2013 at 7:01 am

    It should work fine if you omit the LDFLAGS line and don't bother to
    compile num.c yourself.

    Ian
    When I do this I get the following error when i run go build:

    # command-line-arguments
    /tmp/go-build187266463/command-line-arguments/_obj/lcrypt.cgo2.o: In
    function `_cgo_e13338572e3e_Cfunc_getNum':
    ./lcrypt.go:33: undefined reference to `getNum'
    collect2: error: ld returned 1 exit status

    --
    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.
  • Minux at Oct 22, 2013 at 9:17 am

    On Oct 22, 2013 3:01 AM, wrote:
    It should work fine if you omit the LDFLAGS line and don't bother to
    compile num.c yourself.

    Ian

    When I do this I get the following error when i run go build:

    # command-line-arguments
    /tmp/go-build187266463/command-line-arguments/_obj/lcrypt.cgo2.o: In
    function `_cgo_e13338572e3e_Cfunc_getNum':
    ./lcrypt.go:33: undefined reference to `getNum'

    collect2: error: ld returned 1 exit status
    please post the whole output of
    go build -x

    --
    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.
  • Three Eyed Pike at Oct 22, 2013 at 9:28 am

    please post the whole output of
    go build -x

    $ go build -x num.go
    WORK=/tmp/go-build696498737
    mkdir -p $WORK/command-line-arguments/_obj/
    cd /home/anders/go/src/num
    /home/anders/source/go/pkg/tool/linux_386/cgo -objdir
    $WORK/command-line-arguments/_obj/ -- -I $WORK/command-line-arguments/_obj/
    num.go
    /home/anders/source/go/pkg/tool/linux_386/8c -F -V -w -I
    $WORK/command-line-arguments/_obj/ -I /home/anders/source/go/pkg/linux_386
    -o $WORK/command-line-arguments/_obj/_cgo_defun.8 -D GOOS_linux -D
    GOARCH_386 $WORK/command-line-arguments/_obj/_cgo_defun.c
    gcc -I . -g -O2 -fPIC -m32 -pthread -print-libgcc-file-name
    gcc -I . -g -O2 -fPIC -m32 -pthread -I $WORK/command-line-arguments/_obj/
    -o $WORK/command-line-arguments/_obj/_cgo_main.o -c
    $WORK/command-line-arguments/_obj/_cgo_main.c
    gcc -I . -g -O2 -fPIC -m32 -pthread -I $WORK/command-line-arguments/_obj/
    -o $WORK/command-line-arguments/_obj/_cgo_export.o -c
    $WORK/command-line-arguments/_obj/_cgo_export.c
    gcc -I . -g -O2 -fPIC -m32 -pthread -I $WORK/command-line-arguments/_obj/
    -o $WORK/command-line-arguments/_obj/num.cgo2.o -c
    $WORK/command-line-arguments/_obj/num.cgo2.c
    gcc -I . -g -O2 -fPIC -m32 -pthread -o
    $WORK/command-line-arguments/_obj/_cgo_.o
    $WORK/command-line-arguments/_obj/_cgo_main.o
    $WORK/command-line-arguments/_obj/_cgo_export.o
    $WORK/command-line-arguments/_obj/num.cgo2.o
    # command-line-arguments
    /tmp/go-build696498737/command-line-arguments/_obj/num.cgo2.o: In function
    `_cgo_3f99d1acac32_Cfunc_getNum':
    ./num.go:33: undefined reference to `getNum'
    collect2: error: ld returned 1 exit status


    --
    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.
  • Minux at Oct 22, 2013 at 10:00 am

    On Oct 22, 2013 5:28 AM, wrote:
    please post the whole output of
    go build -x
    $ go build -x num.go
    WORK=/tmp/go-build696498737
    mkdir -p $WORK/command-line-arguments/_obj/
    cd /home/anders/go/src/num
    /home/anders/source/go/pkg/tool/linux_386/cgo -objdir
    $WORK/command-line-arguments/_obj/ -- -I $WORK/command-line-arguments/_obj/
    num.go
    /home/anders/source/go/pkg/tool/linux_386/8c -F -V -w -I
    $WORK/command-line-arguments/_obj/ -I /home/anders/source/go/pkg/linux_386
    -o $WORK/command-line-arguments/_obj/_cgo_defun.8 -D GOOS_linux -D
    GOARCH_386 $WORK/command-line-arguments/_obj/_cgo_defun.c
    gcc -I . -g -O2 -fPIC -m32 -pthread -print-libgcc-file-name
    gcc -I . -g -O2 -fPIC -m32 -pthread -I $WORK/command-line-arguments/_obj/
    -o $WORK/command-line-arguments/_obj/_cgo_main.o -c
    $WORK/command-line-arguments/_obj/_cgo_main.c
    gcc -I . -g -O2 -fPIC -m32 -pthread -I $WORK/command-line-arguments/_obj/
    -o $WORK/command-line-arguments/_obj/_cgo_export.o -c
    $WORK/command-line-arguments/_obj/_cgo_export.c
    gcc -I . -g -O2 -fPIC -m32 -pthread -I $WORK/command-line-arguments/_obj/
    -o $WORK/command-line-arguments/_obj/num.cgo2.o -c
    $WORK/command-line-arguments/_obj/num.cgo2.c
    gcc -I . -g -O2 -fPIC -m32 -pthread -o
    $WORK/command-line-arguments/_obj/_cgo_.o
    $WORK/command-line-arguments/_obj/_cgo_main.o
    $WORK/command-line-arguments/_obj/_cgo_export.o
    $WORK/command-line-arguments/_obj/num.cgo2.o
    # command-line-arguments
    /tmp/go-build696498737/command-line-arguments/_obj/num.cgo2.o: In
    function `_cgo_3f99d1acac32_Cfunc_getNum':
    ./num.go:33: undefined reference to `getNum'

    collect2: error: ld returned 1 exit status
    did you put num.c in the same directory as num.go?

    --
    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.
  • Three Eyed Pike at Oct 22, 2013 at 10:49 am
    did you put num.c in the same directory as num.go?
    yes, mun.c and mun.h is in the same dir as num.go

    --
    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.
  • Minux at Oct 22, 2013 at 11:06 am

    On Tue, Oct 22, 2013 at 6:49 AM, wrote:
    did you put num.c in the same directory as num.go?
    yes, mun.c and mun.h is in the same dir as num.go
    this is strange, the go tool should be able to pick up num.c and compile
    it. i suspect you issued:
    go build num.go
    instead of plain:
    go build

    the first case asks the go tool to only build num.go even if there are
    other source files for the
    same package.

    --
    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.
  • Three Eyed Pike at Oct 22, 2013 at 11:19 am
    this is strange, the go tool should be able to pick up num.c and compile
    it. i suspect you issued:
    go build num.go
    instead of plain:
    go build

    the first case asks the go tool to only build num.go even if there are
    other source files for the
    same package.
    Yes you are correct. Now it works.

    Thanks for the help.

    --
    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
postedOct 21, '13 at 5:11p
activeOct 22, '13 at 11:19a
posts9
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase