static linking to dlib
frpunzalan opened this issue · 6 comments
Hi,
I was trying to check how go-face links to dlib and it seems that it links to a dynamic library. This might be an issue if I want to transfer a binary to another machine (or a docker container) since both dlib and mkl libraries (plus dependencies) needs around ~1 GB of disk space.
Is it possible to link go-face to a statically link to dlib so that we can compile a stand-alone binary?
Should be possible in case of dlib, not sure about mkl. Might try with e.g. openblas instead.
Closing because it's out of scope of go-face.
Should be possible with something like CGO_LDFLAGS="-static"
but don't have time to test this, because you also need static (.a
) version of all libraries that dlib depends on.
@frpunzalan I managed to build go-face application statically in docker.
Its based on Ubuntu 19.04 base image.
Dockerfile.ubuntu19.04
FROM ubuntu:19.04 as base
RUn apt-get update
RUN apt-get update && apt-get install pkg-config libdlib-dev libopenblas-dev libgfortran-8-dev libjpeg-turbo8-dev -y
ADD dlib-1.pc /usr/local/lib/pkgconfig/
RUN pkg-config --cflags -- dlib-1
dlib-1.pc
libdir=/usr/lib/x86_64-linux-gnu
includedir=/usr/include
Name: dlib
Description: Numerical and networking C++ library
Version: 19.10.0
Libs: -L${libdir} -ldlib -lblas -llapack -lgfortran -lquadmath
Cflags: -I${includedir}
Requires:
These two files give you basic environment with dev dependencies ready for static build. I named this image dlibubuntu
Next goes golang 1.13.8 base image with go-face library built in the $GOPATH
Because go-face installation takes much time I decided to make it as separate docker image.
FROM dlibubuntu as builder
RUN apt-get install wget git gcc -y
RUN wget https://dl.google.com/go/go1.13.8.linux-amd64.tar.gz
RUN tar -xf go1.13.8.linux-amd64.tar.gz
RUN mv go /usr/local
ENV GOROOT /usr/local/go
ENV GOPATH /go
ENV PATH $GOPATH/bin:$GOROOT/bin:$PATH
RUN mkdir $GOPATH
RUN go get -v github.com/Kagami/go-face
Last one. Your golang application which is using go-face. Basically app the same as example one. I provide here only its Dockerfile.
FROM goface:latest as builder
WORKDIR /go/src/service
ADD . .
RUN CGO_LDFLAGS="-static" CGO_ENABLED=1 GOOS=linux go build
FROM alpine
WORKDIR /root
COPY --from=builder /go/src/service /root
Run
docker run -it facerecognize:latest /root/service
Nayoung
One restriction here user app should be in GOPATH to re-use go-face package.
I hope this will help someone.
Hello @tpoxa ,
Thanks for your research with Docker.
Reproducing what you've done and fixing little issues I reached to have the example program running in the container (using go run
).
Still, I'm fighting with an issue when trying to build the binary
using CGO_LDFLAGS="-static" CGO_ENABLED=1 GOOS=linux go build
Does the following error is bringing some bells to you?
(.text+0x9cd): undefined reference to `_gfortran_concat_string'
(.text+0x781): more undefined references to
_gfortran_concat_string' follow
collect2`
i have the same error