Struggling to compile Go bindings #190

Closed
opened 2026-01-29 20:39:33 +00:00 by claunia · 12 comments
Owner

Originally created by @pzduniak on GitHub (Aug 17, 2017).

Alpine 3.6, any go build results in the following:

# github.com/google/brotli/go/cbrotli
//usr/local/lib/libbrotlidec.a(state.c.o): In function `BrotliDecoderStateInitWithCustomAllocators':
/usr/local/brotli/c/dec/state.c:56: undefined reference to `BrotliGetDictionary'
//usr/local/lib/libbrotlienc.a(encode.c.o): In function `EncodeData':
/usr/local/brotli/c/enc/encode.c:866: undefined reference to `BrotliGetDictionary'
//usr/local/lib/libbrotlienc.a(encode.c.o): In function `BrotliCompressBufferQuality10':
/usr/local/brotli/c/enc/encode.c:1103: undefined reference to `BrotliGetDictionary'
collect2: error: ld returned 1 exit status

Brotli is built using the following command in a Dockerfile:

RUN cd /usr/local && git clone https://github.com/google/brotli && \
	cd brotli && mkdir out && cd out && ../configure-cmake \
	--disable-shared-libs && make && make install

I have no idea what I should do, I am unable to reproduce it on OSX, local Alpine VM and so on. Whatever I do during the CI process (it's on CircleCI), this happens.

Originally created by @pzduniak on GitHub (Aug 17, 2017). Alpine 3.6, any `go build` results in the following: ``` # github.com/google/brotli/go/cbrotli //usr/local/lib/libbrotlidec.a(state.c.o): In function `BrotliDecoderStateInitWithCustomAllocators': /usr/local/brotli/c/dec/state.c:56: undefined reference to `BrotliGetDictionary' //usr/local/lib/libbrotlienc.a(encode.c.o): In function `EncodeData': /usr/local/brotli/c/enc/encode.c:866: undefined reference to `BrotliGetDictionary' //usr/local/lib/libbrotlienc.a(encode.c.o): In function `BrotliCompressBufferQuality10': /usr/local/brotli/c/enc/encode.c:1103: undefined reference to `BrotliGetDictionary' collect2: error: ld returned 1 exit status ``` Brotli is built using the following command in a Dockerfile: ``` RUN cd /usr/local && git clone https://github.com/google/brotli && \ cd brotli && mkdir out && cd out && ../configure-cmake \ --disable-shared-libs && make && make install ``` I have no idea what I should do, I am unable to reproduce it on OSX, local Alpine VM and so on. Whatever I do during the CI process (it's on CircleCI), this happens.
Author
Owner

@pzduniak commented on GitHub (Aug 17, 2017):

Running with -x:

gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches \
-o $WORK/bitbucket.org/portablecloud/cube/vendor/github.com/google/brotli/go/cbrotli/_obj/_cgo_.o \
$WORK/bitbucket.org/portablecloud/cube/vendor/github.com/google/brotli/go/cbrotli/_obj/_cgo_main.o \
$WORK/bitbucket.org/portablecloud/cube/vendor/github.com/google/brotli/go/cbrotli/_obj/_cgo_export.o \
$WORK/bitbucket.org/portablecloud/cube/vendor/github.com/google/brotli/go/cbrotli/_obj/cgo.cgo2.o \
$WORK/bitbucket.org/portablecloud/cube/vendor/github.com/google/brotli/go/cbrotli/_obj/reader.cgo2.o \
$WORK/bitbucket.org/portablecloud/cube/vendor/github.com/google/brotli/go/cbrotli/_obj/writer.cgo2.o \
-g -O2 -lbrotlicommon -lbrotlidec -lbrotlienc
@pzduniak commented on GitHub (Aug 17, 2017): Running with `-x`: ``` gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches \ -o $WORK/bitbucket.org/portablecloud/cube/vendor/github.com/google/brotli/go/cbrotli/_obj/_cgo_.o \ $WORK/bitbucket.org/portablecloud/cube/vendor/github.com/google/brotli/go/cbrotli/_obj/_cgo_main.o \ $WORK/bitbucket.org/portablecloud/cube/vendor/github.com/google/brotli/go/cbrotli/_obj/_cgo_export.o \ $WORK/bitbucket.org/portablecloud/cube/vendor/github.com/google/brotli/go/cbrotli/_obj/cgo.cgo2.o \ $WORK/bitbucket.org/portablecloud/cube/vendor/github.com/google/brotli/go/cbrotli/_obj/reader.cgo2.o \ $WORK/bitbucket.org/portablecloud/cube/vendor/github.com/google/brotli/go/cbrotli/_obj/writer.cgo2.o \ -g -O2 -lbrotlicommon -lbrotlidec -lbrotlienc ```
Author
Owner

@eustas commented on GitHub (Aug 23, 2017):

Thanks for the report. Going to investigate/fix soon

@eustas commented on GitHub (Aug 23, 2017): Thanks for the report. Going to investigate/fix soon
Author
Owner

@eustas commented on GitHub (Aug 23, 2017):

Could you provide sample CircleCI configuration, please?

@eustas commented on GitHub (Aug 23, 2017): Could you provide sample CircleCI configuration, please?
Author
Owner

@eustas commented on GitHub (Sep 19, 2017):

Hi. Could you try this once again? Now both static and shared libraries are built.
cgo.go will hook up with shared libraries, though.

@eustas commented on GitHub (Sep 19, 2017): Hi. Could you try this once again? Now both static and shared libraries are built. cgo.go will hook up with shared libraries, though.
Author
Owner

@jbergstroem commented on GitHub (Mar 2, 2018):

I tried this with golang:1.10-alpine by installing the build deps, building and installing brotli then verifying with go test:

~/brotli/go/cbrotli # go test
PASS
ok      _/root/brotli/go/cbrotli        0.101s
@jbergstroem commented on GitHub (Mar 2, 2018): I tried this with `golang:1.10-alpine` by installing the build deps, building and installing brotli then verifying with `go test`: ```console ~/brotli/go/cbrotli # go test PASS ok _/root/brotli/go/cbrotli 0.101s ```
Author
Owner

@pzduniak commented on GitHub (Mar 2, 2018):

We've actually been using a fork of Brotli in our org, I'll look into it this week.

@pzduniak commented on GitHub (Mar 2, 2018): We've actually been using a fork of Brotli in our org, I'll look into it this week.
Author
Owner

@pzduniak commented on GitHub (Mar 2, 2018):

I'm actually building static libraries and here's where the issue happens. Dynamic build works fine. From what I understand the static libraries are also built when you run a build through CMake, so it should be working fine.

go install '-linkmode external -extldflags "-static"' -v ./...
@pzduniak commented on GitHub (Mar 2, 2018): I'm actually building static libraries and here's where the issue happens. Dynamic build works fine. From what I understand the static libraries are also built when you run a build through CMake, so it should be working fine. ``` go install '-linkmode external -extldflags "-static"' -v ./... ```
Author
Owner

@pzduniak commented on GitHub (Mar 2, 2018):

And here are the errors:

/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/lib/gcc/x86_64-alpine-linux-musl/6.4.0/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -lbrotlicommon
/usr/lib/gcc/x86_64-alpine-linux-musl/6.4.0/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -lbrotlidec
/usr/lib/gcc/x86_64-alpine-linux-musl/6.4.0/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -lbrotlienc
@pzduniak commented on GitHub (Mar 2, 2018): And here are the errors: ``` /usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1 /usr/lib/gcc/x86_64-alpine-linux-musl/6.4.0/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -lbrotlicommon /usr/lib/gcc/x86_64-alpine-linux-musl/6.4.0/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -lbrotlidec /usr/lib/gcc/x86_64-alpine-linux-musl/6.4.0/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -lbrotlienc ```
Author
Owner

@eustas commented on GitHub (Oct 24, 2018):

Look like "static" libraries have to be installed / copied in specific locations so that compiler could find them...

Is it possible to create a clean repro case - i.e. script that could end up in compilation failure, similar to this or this?

@eustas commented on GitHub (Oct 24, 2018): Look like "static" libraries have to be installed / copied in specific locations so that compiler could find them... Is it possible to create a clean repro case - i.e. script that could end up in compilation failure, similar to [this](https://github.com/google/brotli/wiki/Building-Windows-artifacts) or [this](https://github.com/google/brotli/wiki/Runnung-Coverity-Scan)?
Author
Owner

@krk commented on GitHub (Mar 6, 2019):

A workaround is to remove the #cgo comments from 9cd01c0437/go/cbrotli/cgo.go (L10-L12) and specify the static libraries in CGO_LDFLAGS:

sed -i '/#cgo/d' vendor/github.com/google/brotli/go/cbrotli/cgo.go
CGO_LDFLAGS="-lbrotlidec-static -lbrotlienc-static -lbrotlicommon-static -lm -static" go install -v ./...
@krk commented on GitHub (Mar 6, 2019): A workaround is to remove the `#cgo` comments from https://github.com/google/brotli/blob/9cd01c0437e8b6010434d3491a348a5645de624b/go/cbrotli/cgo.go#L10-L12 and specify the static libraries in `CGO_LDFLAGS`: ``` sed -i '/#cgo/d' vendor/github.com/google/brotli/go/cbrotli/cgo.go CGO_LDFLAGS="-lbrotlidec-static -lbrotlienc-static -lbrotlicommon-static -lm -static" go install -v ./... ```
Author
Owner

@jameshartig commented on GitHub (Jan 23, 2020):

Is there a reason the static libraries have a -static suffix? If they didn't have that then the cgo comments would work, right? Alternatively we could make another cgo_static file that has build constraints to only be used when static_build tag is present.

@jameshartig commented on GitHub (Jan 23, 2020): Is there a reason the static libraries have a `-static` suffix? If they didn't have that then the cgo comments would work, right? Alternatively we could make another cgo_static file that has build constraints to only be used when `static_build` tag is present.
Author
Owner

@eustas commented on GitHub (Dec 15, 2021):

It is a build system limitation. We can not use both dynamic and static library from one target...

@eustas commented on GitHub (Dec 15, 2021): It is a build system limitation. We can not use both dynamic and static library from one target...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/brotli#190