gen2brain / go-fitz

Golang wrapper for the MuPDF Fitz library
GNU Affero General Public License v3.0
369 stars 87 forks source link

undefined: fitz.New #102

Closed MichaelCharles closed 5 months ago

MichaelCharles commented 5 months ago

I'm trying to make a lambda function with Serverless framework. When I attempt to build the project, I'm getting the following output:

➜ make build
go mod tidy
export GO111MODULE=on
env GOARCH=amd64 GOOS=linux go build -ldflags="-s -w" -o bin/thumbnail_on_upload cmd/thumbnail_on_upload/main.go
# bitbucket.org/acmecorp/sls-thumbnail/internal/conversion
internal/conversion/conversion.go:26:19: undefined: fitz.New
make: *** [build] Error 1

I made a separate, more simple project, and attempt to build it with go build and had no problems. So I tried adding the same build conditions as I'm using on the Serverless project until I reproduced the same behavior. I've narrowed it down to just the env GOOS=linux setting.

➜ env GOOS=linux go build
# thumbnails
./main.go:56:19: undefined: fitz.New

I'm on an M1 MacBook Pro attempting to build for Linux (to deploy to AWS Lambda). I'm using go 1.21.6. Thank you for your time. Any guidance would be much appreciated.

gen2brain commented 5 months ago

You are trying to cross-compile the CGO project. Of course, you cannot just change GOOS and be done. When you cross-compile, the Go will disable CGO, i.e. it will set CGO_ENABLED=0, so enable it, i.e. GOOS=linux CGO_ENABLED=1. Then you will see real errors, i.e. you are missing the correct C toolchain.

MichaelCharles commented 5 months ago

Thanks, I made further progress on this since I last commented. My problem has nothing to do with Fitz itself, but as you said, cross compiling. I'd like to post what I ended up doing here in case it ever turns up in someone's Google search results.

I figured the easiest way to get around this would be to build from inside of a Docker container. So I made a Dockerfile like so,

FROM golang:1.21.6

RUN apt-get update

RUN apt-get install -y gcc-x86-64-linux-gnu

ENV CGO_ENABLED=1
ENV CC=x86_64-linux-gnu-gcc

Which includes gcc-x86-64-linux-gnu as a cross compiler, and then built the dockerfile into an image:

docker build -t gobuild:latest .

From there, I used the new image to build my project:

docker run --rm -v "$(CURDIR)":/usr/src/myapp -w /usr/src/myapp gobuild:latest bash -c "export GO111MODULE=on && env GOARCH=amd64 go build  -ldflags=\"-s -w\" -o bin/thumbnail cmd/thumbnail/main.go"

And that let me build without errors! I'm running into a further problem there with my serverless function hanging on invocation, but that is likely an entirely unrelated issue.

gen2brain commented 5 months ago

You could also use e.g. zig, something like this CGO_ENABLED=1 CC="zig cc -target x86_64-windows-gnu" GOOS=windows, or maybe with clang, i.e. CC="clang --target=x86_64-pc-freebsd" CGO_ENABLED=1 GOOS=freebsd. In go-fitz case, there are no additional libraries that you need to have, all it needs is to link precompiled static library.