"undefined reference" when linking C-library using gccgo

747 views
Skip to first unread message

fredrik.pe...@gmail.com

unread,
Mar 14, 2014, 6:53:03 AM3/14/14
to golan...@googlegroups.com
Hello everyone!

Today I encountered what seems to be a bug in gccgo.

What I'm trying to do is to link and use a C-library in my application written in Go. I have narrowed the C-library down to having a method printing "hi" on stdio.

The directory structure is:

$ tree cfuncer ccombo
cfuncer
├── inc
│   └── cfuncer.h
├── libcfunker.a
└── src
    └── cfuncer.c
ccombo
└── ccombo.go

The main Go-program looks like:


the C-library consists of the following code:


and is compiled in the following way:

$ cd cfunker
$ gcc -I inc -Wall -c cfuncer.c
$ ar cruv libcfuncer.a cfuncer.o

Compiling the main program as:

$ cd ../ccombo
$ go build main.go

works, but when using gccgo like:

$ go build -compiler=gccgo main.go

it fails with the error:

# command-line-arguments
/tmp/go-build937064904/command-line-arguments/_obj/ccombo.cgo2.o: In function `_cgo_53b96737dc38_Cfunc_cfunction':
ccombo.cgo2.c:(.text+0x3): undefined reference to `cfunction'
collect2: error: ld returned 1 exit status

My compilers has the following versions:

$ go version
go version go1.2 linux/amd64

$ gccgo -v
Using built-in specs.
COLLECT_GCC=gccgo
COLLECT_LTO_WRAPPER=/opt/gccgo-stable/libexec/gcc/x86_64-unknown-linux-gnu/4.8.2/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-4.8.2/configure --prefix=/opt/gccgo-stable --enable-languages=c,c++,go
Thread model: posix
gcc version 4.8.2 (GCC)

I have also tried to do the same compilation with gcc 4.9.0 but it produces the same error.

$ gccgo -v
Using built-in specs.
COLLECT_GCC=/opt/gccgo/bin/gccgo
COLLECT_LTO_WRAPPER=/opt/gccgo/libexec/gcc/x86_64-unknown-linux-gnu/4.9.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc/configure --prefix=/opt/gccgo --with-arch-32=i586 --enable-languages=c,c++,go
Thread model: posix
gcc version 4.9.0 20131127 (experimental) (GCC)

-- 
Fredrik Pettersson

Ian Lance Taylor

unread,
Mar 14, 2014, 9:42:43 AM3/14/14
to fredrik.pe...@gmail.com, golang-nuts
On Fri, Mar 14, 2014 at 3:53 AM, <fredrik.pe...@gmail.com> wrote:
>
> Today I encountered what seems to be a bug in gccgo.


Is this http://golang.org/issue/7303 ?

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...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

fredrik.pe...@gmail.com

unread,
Mar 17, 2014, 6:22:08 AM3/17/14
to golan...@googlegroups.com, fredrik.pe...@gmail.com
On Friday, March 14, 2014 2:42:43 PM UTC+1, Ian Lance Taylor wrote:
On Fri, Mar 14, 2014 at 3:53 AM,  <fredrik.pe...@gmail.com> wrote:
>
> Today I encountered what seems to be a bug in gccgo.


Is this http://golang.org/issue/7303 ?


I recompiled my gcc today and gccgo now has version 4.9.0 20140307. 

Since the issue was solved March 12 it is not possible for me to try the fix with my compiler (since the gofrontend is not manually merged to gcc including that commit). I Also looked through the issue and it does not seem like it is the same problem as mine.

If it helps my command-line arguments are:

$ go build -x -compiler=gccgo ccombo.go
WORK=/tmp/go-build842442322
mkdir -p $WORK/command-line-arguments/_obj/
cd /home/fredpe/src/goprogs/src/ccombo
/home/fredpe/src/go/pkg/tool/linux_amd64/cgo -objdir $WORK/command-line-arguments/_obj/ -gccgo -- -I $WORK/command-line-arguments/_obj/ -I ../cfunker/inc/ ccombo.go
gcc -Wall -g -I $WORK/command-line-arguments/_obj/ -I /home/fredpe/src/go/pkg/linux_amd64 -o $WORK/command-line-arguments/_obj/_cgo_defun.o -D GOOS_linux -D GOARCH_amd64 -m64 -c $WORK/command-line-arguments/_obj/_cgo_defun.c
gcc -I . -g -O2 -fPIC -m64 -pthread -print-libgcc-file-name
gcc -I . -g -O2 -fPIC -m64 -pthread -I $WORK/command-line-arguments/_obj/ -I ../cfunker/inc/ -o $WORK/command-line-arguments/_obj/_cgo_main.o -c $WORK/command-line-arguments/_obj/_cgo_main.c
gcc -I . -g -O2 -fPIC -m64 -pthread -I $WORK/command-line-arguments/_obj/ -I ../cfunker/inc/ -o $WORK/command-line-arguments/_obj/_cgo_export.o -c $WORK/command-line-arguments/_obj/_cgo_export.c
gcc -I . -g -O2 -fPIC -m64 -pthread -I $WORK/command-line-arguments/_obj/ -I ../cfunker/inc/ -o $WORK/command-line-arguments/_obj/ccombo.cgo2.o -c $WORK/command-line-arguments/_obj/ccombo.cgo2.c
gcc -I . -g -O2 -fPIC -m64 -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/ccombo.cgo2.o -L ../cfunker/ -lcfunker
gccgo -I $WORK -c -g -m64 -fgo-relative-import-path=_/home/fredpe/src/goprogs/src/ccombo -o $WORK/command-line-arguments/_obj/main.o $WORK/command-line-arguments/_obj/_cgo_gotypes.go $WORK/command-line-arguments/_obj/ccombo.cgo1.go
ar cru $WORK/libcommand-line-arguments.a $WORK/command-line-arguments/_obj/main.o $WORK/command-line-arguments/_obj/_cgo_defun.o $WORK/command-line-arguments/_obj/_cgo_export.o $WORK/command-line-arguments/_obj/ccombo.cgo2.o
cd .
gccgo -o ccombo $WORK/command-line-arguments/_obj/main.o $WORK/command-line-arguments/_obj/_cgo_defun.o $WORK/command-line-arguments/_obj/_cgo_export.o $WORK/command-line-arguments/_obj/ccombo.cgo2.o -Wl,-( -m64 -lpthread -Wl,-E -Wl,-)
# command-line-arguments
/tmp/go-build842442322/command-line-arguments/_obj/ccombo.cgo2.o: In function `_cgo_ec8ce97ff4d4_Cfunc_cfunction':
ccombo.cgo2.c:(.text+0x3): undefined reference to `cfunction'
collect2: error: ld returned 1 exit status 

-- 
Fredrik Pettersson

Ian Lance Taylor

unread,
Mar 17, 2014, 3:20:11 PM3/17/14
to Fredrik Pettersson, golang-nuts
On Mon, Mar 17, 2014 at 3:22 AM, <fredrik.pe...@gmail.com> wrote:
>
> Since the issue was solved March 12 it is not possible for me to try the fix
> with my compiler (since the gofrontend is not manually merged to gcc
> including that commit).

That is a change to the go tool, which is not included with gccgo
anyhow.


> I Also looked through the issue and it does not seem
> like it is the same problem as mine.

That is of course possible.

Ian

Ian Lance Taylor

unread,
Mar 17, 2014, 3:20:49 PM3/17/14
to Fredrik Pettersson, golang-nuts
Could you open an issue for this at http://golang.org/issue/new with a
complete standalone test case? Thanks.

Ian

fredrik.pe...@gmail.com

unread,
Mar 18, 2014, 6:03:14 AM3/18/14
to golan...@googlegroups.com, Fredrik Pettersson
On Monday, March 17, 2014 8:20:49 PM UTC+1, Ian Lance Taylor wrote:
Could you open an issue for this at http://golang.org/issue/new with a
complete standalone test case?  Thanks.


Thanks for your time and help!

--
Fredrik Pettersson
Reply all
Reply to author
Forward
0 new messages