Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: undefined reference when linking a C-library with gccgo #7573

Closed
gopherbot opened this issue Mar 18, 2014 · 6 comments
Closed

cmd/cgo: undefined reference when linking a C-library with gccgo #7573

gopherbot opened this issue Mar 18, 2014 · 6 comments
Milestone

Comments

@gopherbot
Copy link

by fredrik.pettersson.89:

What does 'go version' print?

$ go version
go version devel +06f148a92fbb Tue Mar 18 16:26:23 2014 +1100 linux/amd64

gccgo version:

$ gccgo -v
Using built-in specs.
COLLECT_GCC=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 --enable-languages=c,c++,go
Thread model: posix
gcc version 4.9.0 20140307 (experimental) (GCC)

What steps reproduce the problem?
If possible, include a link to a program on play.golang.org.

1. The directory structure is:

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

cfuncer.h
http://play.golang.org/p/dyDC_qFTph

cfuncer.c
http://play.golang.org/p/BIVXB0wZjg

ccombo.go
http://play.golang.org/p/h9cXKM6WzG

2. C-library compilation:

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

3. ccombo.go compilation:

$ cd ../ccombo
$ go build -compiler=gccgo ccombo.go

What happened?

It produces the output: 

# command-line-arguments
/tmp/go-build736128395/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

What should have happened instead?

There should be no output, instead a binary should be created.

Please provide any additional information below.

$ go build -x -compiler=gccgo ccombo.go
WORK=/tmp/go-build814662068
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 . -fPIC -m64 -pthread -fmessage-length=0 -print-libgcc-file-name
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -I $WORK/command-line-arguments/_obj/ -g
-O2 -I ../cfunker/inc/ -o $WORK/command-line-arguments/_obj/_cgo_main.o -c
$WORK/command-line-arguments/_obj/_cgo_main.c
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -I $WORK/command-line-arguments/_obj/ -g
-O2 -I ../cfunker/inc/ -o $WORK/command-line-arguments/_obj/_cgo_export.o -c
$WORK/command-line-arguments/_obj/_cgo_export.c
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -I $WORK/command-line-arguments/_obj/ -g
-O2 -I ../cfunker/inc/ -o $WORK/command-line-arguments/_obj/ccombo.cgo2.o -c
$WORK/command-line-arguments/_obj/ccombo.cgo2.c
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -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 -g -O2 -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-build814662068/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
@ianlancetaylor
Copy link
Contributor

Comment 1:

Does this work when using the gc compiler?

Labels changed: added repo-main.

@ianlancetaylor
Copy link
Contributor

Comment 2:

Labels changed: added release-go1.3.

@gopherbot
Copy link
Author

Comment 4 by erik.westrup:

Yes, gc works.

@gopherbot
Copy link
Author

Comment 5 by fredrik.pettersson.89:

In the last command-line argument it seems like gccgo does not link the library.
Adding -gccgoflags="-L ../cfuncer/ -lcfuncer" to the build command so it looks like:
go build -compiler=gccgo -gccgoflags="-L ../cfuncer/ -lcfuncer" ccombo.go
changes the last command to:
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,-) -L
../cfuncer/ -lcfuncer
This results in no error and a working binary is produced.

@gopherbot
Copy link
Author

Comment 6 by erik.westrup:

Proposed patch: https://golang.org/cl/80780043/

@ianlancetaylor
Copy link
Contributor

Comment 7:

This issue was closed by revision 881d693.

Status changed to Fixed.

@rsc rsc added this to the Go1.3 milestone Apr 14, 2015
@rsc rsc removed the release-go1.3 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants