go build error in darwin: duplicate symbol

1,258 views
Skip to first unread message

Max Xu

unread,
Mar 18, 2021, 12:55:35 PM3/18/21
to golang-nuts
Hi all,

I'm running into a weird bug:

Failed with command:
GOOS=darwin go build

Bug success with:
GOOS=linux go build

The failed Message:

/usr/local/opt/go/libexec/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
duplicate symbol '_readdrivestat' in:
    /var/folders/tx/5c8vkq6x2k1f5nqvbj271qyc0000gn/T/go-link-162098608/000011.o
    /var/folders/tx/5c8vkq6x2k1f5nqvbj271qyc0000gn/T/go-link-162098608/000036.o
duplicate symbol '_get_temperature' in:
    /var/folders/tx/5c8vkq6x2k1f5nqvbj271qyc0000gn/T/go-link-162098608/000014.o
    /var/folders/tx/5c8vkq6x2k1f5nqvbj271qyc0000gn/T/go-link-162098608/000039.o
duplicate symbol '_open_smc' in:
    /var/folders/tx/5c8vkq6x2k1f5nqvbj271qyc0000gn/T/go-link-162098608/000014.o
    /var/folders/tx/5c8vkq6x2k1f5nqvbj271qyc0000gn/T/go-link-162098608/000039.o
duplicate symbol '_close_smc' in:
    /var/folders/tx/5c8vkq6x2k1f5nqvbj271qyc0000gn/T/go-link-162098608/000014.o
    /var/folders/tx/5c8vkq6x2k1f5nqvbj271qyc0000gn/T/go-link-162098608/000039.o
ld: 4 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


My Environment:

go version go1.16.2 darwin/amd64

Ian Lance Taylor

unread,
Mar 18, 2021, 5:11:57 PM3/18/21
to Max Xu, golang-nuts
Hard to say without seeing the code, but my first guess would be
erroneous cgo code. It looks like you have multiple cases of cgo code
that are defining the same symbol in the C preamble without declaring
it as `static`.

Ian

Max Xu

unread,
Mar 22, 2021, 9:02:36 AM3/22/21
to golang-nuts
But I didn't write cgo code in my project. 
The weird part is I can build success with GOOS=linux go build, but failed with GOOS=darwin go build

Alex

unread,
Mar 22, 2021, 10:40:27 AM3/22/21
to golang-nuts
A package you import could be using cgo, you don't have to be using it directly.
If you run `GOOS=linux go build` on a mac, that is cross compileing and cgo is disabled by default.
You can try `CGO_ENABLED=0 go build` and if it works then it is cgo related.

From the error messages, it looks like a package for using a SMC or other hardware.

Ian Lance Taylor

unread,
Mar 22, 2021, 12:25:17 PM3/22/21
to Alex, golang-nuts
On Mon, Mar 22, 2021 at 7:40 AM Alex <alex....@gmail.com> wrote:
>
> A package you import could be using cgo, you don't have to be using it directly.

I'll add that I think that this must be what is happening, as you
could not get that error message in a program that doesn't use cgo
anywhere.

Ian

Xie Zhenye

unread,
Jan 9, 2023, 10:21:00 PM1/9/23
to golang-nuts
It's caused by github.com/lufia/iostat.

Maybe two different dependencies used 'iostat'. 'iostat; used cgo, and produced an object file from .c file for each dependency. Linker try to link both .o file that cause the problem.

Xie Zhenye

unread,
Jan 9, 2023, 10:22:39 PM1/9/23
to golang-nuts
Maybe cgo should build a single .o file for the same .c file.
Reply all
Reply to author
Forward
0 new messages