How do you include a C library (libxml2) when building a Go project for Linux?-ThrowExceptions

Exception or error:

I have a Go project which I want to build as a binary so that I can deploy it as an AWS Lambda. It uses a Go wrapper for libxml2 (lestrrat-go) which requires you to have libxml2 installed locally, which I have done via brew install libxml2. The wrapper is installed as a git submodule in the project.

I can run unit tests fine with the following bash script:

export LDFLAGS="-L/usr/local/opt/libxml2/lib"
export CPPFLAGS="-I/usr/local/opt/libxml2/include"
export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig"

go test ./...

However I cannot work out how to build my project as a binary so it can be deployed.

I am working on a Mac and have installed the musl-cross cross compiler to try and compile the project for linux, which is the OS used on AWS Lambda.

My current build script is as follows:

export LDFLAGS="-L/usr/local/opt/libxml2/lib"
export CPPFLAGS="-I/usr/local/opt/libxml2/include"
export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig"
export CGO_ENABLED=1
export CC=x86_64-linux-musl-gcc

GOOS=linux GOARCH=amd64 go build -ldflags="-w -L /usr/local/opt/libxml2/lib" -o bin/main

The command fails with the following output:

/var/folders/_j/dtvtwt3902x_z20282yqmwy00000gp/T/go-build660707780/b125/_x002.o: In function `MY_setXmlIndentTreeOutput':
vendor/github.com/lestrrat-go/libxml2/clib/clib.go:73: undefined reference to `__xmlIndentTreeOutput'
vendor/github.com/lestrrat-go/libxml2/clib/clib.go:74: undefined reference to `__xmlIndentTreeOutput'
/var/folders/_j/dtvtwt3902x_z20282yqmwy00000gp/T/go-build660707780/b125/_x002.o: In function `MY_test_node_name':
vendor/github.com/lestrrat-go/libxml2/clib/clib.go:163: undefined reference to `xmlIsCombiningGroup'
vendor/github.com/lestrrat-go/libxml2/clib/clib.go:163: undefined reference to `xmlCharInRange'
vendor/github.com/lestrrat-go/libxml2/clib/clib.go:163: undefined reference to `xmlIsExtenderGroup'
...  many more of these
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *