Skip to content

cgo: multiple definition of cgo functions error when linking go-compiled c-archive library #20639

@reusee

Description

@reusee

What version of Go are you using (go version)?

go version devel +c99483feb8 Sat Jun 3 17:04:56 2017 +0000 linux/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/reus"
GORACE=""
GOROOT="/home/reus/go"
GOTOOLDIR="/home/reus/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build884024617=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

lib.go

package main

import (
	"C"
	"fmt"
)

func main() {
}

//export Foo
func Foo() {
	fmt.Printf("Foo!\n")
}

lib.c

extern void Foo();

void foo() {
  Foo();
}

build static library

 go build -buildmode=c-archive -o foo.a 

call.go

package main

/*
extern void foo();

#cgo LDFLAGS: ./foo.a
*/
import "C"

func main() {
	C.foo()
}

build call.go

go build call.go

What did you expect to see?

no build error

What did you see instead?

# command-line-arguments
./foo.a(go.o): In function `_cgo_topofstack':
/home/reus/go/src/runtime/asm_amd64.s:2327: multiple definition of `_cgo_topofstack'
/tmp/go-build074487992/command-line-arguments/_obj/_cgo_main.o:/tmp/go-build/command-line-arguments/_obj/_cgo_main.c:5: first defined here
./foo.a(go.o): In function `_cgo_panic':
/home/reus/go/src/runtime/cgo/callbacks.go:45: multiple definition of `_cgo_panic'
/tmp/go-build074487992/command-line-arguments/_obj/_cgo_main.o:/tmp/go-build/command-line-arguments/_obj/_cgo_main.c:6: first defined here
./foo.a(go.o): In function `crosscall2':
/home/reus/go/src/runtime/cgo/asm_amd64.s:12: multiple definition of `crosscall2'
/tmp/go-build074487992/command-line-arguments/_obj/_cgo_main.o:/tmp/go-build/command-line-arguments/_obj/_cgo_main.c:2: first defined here
./foo.a(000001.o): In function `_cgo_wait_runtime_init_done':
/home/reus/go/src/runtime/cgo/gcc_libinit.c:35: multiple definition of `_cgo_wait_runtime_init_done'
/tmp/go-build074487992/command-line-arguments/_obj/_cgo_main.o:/tmp/go-build/command-line-arguments/_obj/_cgo_main.c:3: first defined here
./foo.a(000001.o): In function `_cgo_release_context':
/home/reus/go/src/runtime/cgo/gcc_context.c:11: multiple definition of `_cgo_release_context'
/tmp/go-build074487992/command-line-arguments/_obj/_cgo_main.o:/tmp/go-build/command-line-arguments/_obj/_cgo_main.c:4: first defined here
collect2: error: ld returned 1 exit status

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions