Skip to content

cgo: duplicate definition linking cgo program with c-archive cgo library #49256

@hkloudou

Description

@hkloudou

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

$ go version
go version go1.17.2 darwin/amd64

Does this issue reproduce with the latest release?

yes

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

Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/undefined/Library/Caches/go-build"
GOENV="/Users/undefined/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE="gitlab.me"
GOMODCACHE="/Users/undefined/go/pkg/mod"
GONOPROXY="code.aliyun.com,gitlab.me"
GONOSUMDB="code.aliyun.com,gitlab.me"
GOOS="darwin"
GOPATH="/Users/undefined/go"
GOPRIVATE="code.aliyun.com,gitlab.me"
GOPROXY="https://mirrors.aliyun.com/goproxy/,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17.2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/k5/x_cn68sn66756hdl_ngxghz40000gn/T/go-build2988694177=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

//hello.go
package main

import "C"
import "fmt"

func main() {
// Hello("hello")
}

//export Hello
func Hello() {
fmt.Println("output:")
}

//run.go
package main

/*
#include "libhello.h"
#cgo CFLAGS: -I./dist/
#cgo LDFLAGS: -L./dist/ -lhello
*/
import "C"

func main() {
C.Hello()
}

step1

go build --buildmode=c-archive -o ./dist/libhello.so hello.go

step2

go run run.go

What did you expect to see?

output:

What did you see instead?

duplicate symbol '__cgo_panic' in:
$WORK/b001/_cgo_main.o
./dist/libhello.so(go.o)
duplicate symbol '__cgo_topofstack' in:
$WORK/b001/_cgo_main.o
./dist/libhello.so(go.o)
duplicate symbol '_crosscall2' in:
$WORK/b001/_cgo_main.o
./dist/libhello.so(go.o)
duplicate symbol '__cgo_wait_runtime_init_done' in:
$WORK/b001/_cgo_main.o
./dist/libhello.so(000006.o)
ld: 4 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions