Skip to content

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

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

Closed
hkloudou opened this issue Nov 1, 2021 · 10 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@hkloudou
Copy link

hkloudou commented Nov 1, 2021

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)

@thanm thanm added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 1, 2021
@thanm thanm added this to the Backlog milestone Nov 1, 2021
@thanm thanm changed the title panic,when golang call c-archive/c-shared[go build -buildmode=c-archive] cgo: duplicate definition linking cgo program with c-archive cgo library Nov 1, 2021
@thanm
Copy link
Contributor

thanm commented Nov 1, 2021

I am not sure whether CGO is designed to support this use case. Both the main program and the c-archive library here use CGO. Also interesting in that this error is taking place before we ever invoke the Go linker.

@ianlancetaylor might be able to offer some insights here.

@ianlancetaylor
Copy link
Contributor

Code built with -buildmode=c-archive is intended to be linked into a C program. It doesn't make sense to use -buildmode=c-archive and link the result into a Go program. I'm not sure what you are trying to do, but this won't work. Sorry.

@hkloudou
Copy link
Author

hkloudou commented Nov 2, 2021

@ianlancetaylor it's very useful to me,ex:package some different version c-shared library in docker.

@ianlancetaylor
Copy link
Contributor

Perhaps it is useful (although I don't see why), but that doesn't really matter considering that I don't see any way to implement it. As you observe, it doesn't work, and there really isn't any way to make it work.

@hkloudou
Copy link
Author

hkloudou commented Nov 3, 2021

@ianlancetaylor

thanks your reply
but,it work,if i remove go inner package like fmt,time

//hello.go
package main

import "C"

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

//export Hello
func Hello() int{
return 1
}

@ianlancetaylor
Copy link
Contributor

Yes, there are very limited cases in which it can work. But in general it won't.

@jerson
Copy link

jerson commented Mar 4, 2022

Hi, I have a similar scenario where I have 2 libraries build with cgo as c-archive for 2 different flutter libraries, and when both are installed in the same project return this error


duplicate symbol '__cgo_panic' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(go.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(go.o)
duplicate symbol '__cgo_topofstack' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(go.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(go.o)
duplicate symbol '_crosscall2' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(go.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(go.o)
duplicate symbol '__cgo_release_context' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(000004.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(000004.o)
duplicate symbol '__cgo_sys_thread_start' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(000005.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(000005.o)
duplicate symbol '_x_cgo_init' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(000005.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(000005.o)
duplicate symbol '_x_cgo_set_context_function' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(000006.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(000006.o)
duplicate symbol '__cgo_get_context_function' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(000006.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(000006.o)
duplicate symbol '__cgo_try_pthread_create' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(000006.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(000006.o)
duplicate symbol '_x_cgo_sys_thread_create' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(000006.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(000006.o)
duplicate symbol '_x_cgo_notify_runtime_init_done' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(000006.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(000006.o)
duplicate symbol '__cgo_wait_runtime_init_done' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(000006.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(000006.o)
duplicate symbol '_x_cgo_unsetenv' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(000007.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(000007.o)
duplicate symbol '_x_cgo_setenv' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(000007.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(000007.o)
duplicate symbol '_x_cgo_callers' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(000008.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(000008.o)
duplicate symbol '_x_cgo_thread_start' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(000009.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(000009.o)
duplicate symbol '__cgo_yield' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(000009.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(000009.o)
duplicate symbol '_crosscall_amd64' in:
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/fast_rsa/ios/librsa_bridge.a(000010.o)
    /Users/usuario/Desktop/sample_flutter/ios/.symlinks/plugins/openpgp/ios/libopenpgp_bridge.a(000010.o)
ld: 18 duplicate symbols for architecture x86_64

@rumnatsf
Copy link

rumnatsf commented Feb 3, 2023

I have the same issue. I have 2 different go libraries compiled into ios arm64 static libraries and I link both to the same project. Both have go runtime symbols and therefore conflict when copying to a final app binary

@lcmmhcc
Copy link

lcmmhcc commented Mar 27, 2023

I have the same issue. I have 2 different go libraries compiled into ios arm64 static libraries and I link both to the same project. Both have go runtime symbols and therefore conflict when copying to a final app binary

I have the same issue in the same scenerio. could u plz tell me how u fix it. ?

@rumnatsf
Copy link

We went with a dynamic wrapper over a static library so we had 1 static and 1 dynamic. Go runtime started crashing and we decided to move all libraries to a go-client monorepo and have them merged into one static framework. It will allow us to introduce more go libraries that we share across BE, FE, Android and iOS. FE uses wasm, BE directly imports dependencies from the monorepo, ios is compiled in 1 big static library, android is compiled in 1 big dynamic library

@golang golang locked and limited conversation to collaborators Mar 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

7 participants