Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building with -extldflags=-static #339

Open
Janso123 opened this issue Dec 14, 2022 · 9 comments
Open

Building with -extldflags=-static #339

Janso123 opened this issue Dec 14, 2022 · 9 comments

Comments

@Janso123
Copy link

Hi,
Is there an option to build it with a static flag?

That's my Dockerfile and it core-dumps on me or throws linker error
`FROM golang:alpine as builder

RUN apk add musl-dev vips-dev gcc

WORKDIR /src
COPY . .

RUN go build -o /bin/app --ldflags '-linkmode external -extldflags=-static' . `

@sonu27
Copy link

sonu27 commented May 5, 2023

@Janso123 did you manage to solve it? I have the same issue

@sonu27
Copy link

sonu27 commented May 8, 2023

@AttilaTheFun @tonimelisma have you guys been able to build a static go binary with libvips?

/usr/local/go/pkg/tool/linux_arm64/link: running gcc failed: exit status 1
/usr/lib/gcc/aarch64-alpine-linux-musl/12.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: cannot find -lvips: No such file or directory
/usr/lib/gcc/aarch64-alpine-linux-musl/12.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: cannot find -lgio-2.0: No such file or directory
/usr/lib/gcc/aarch64-alpine-linux-musl/12.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: cannot find -lgobject-2.0: No such file or directory
/usr/lib/gcc/aarch64-alpine-linux-musl/12.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: cannot find -lglib-2.0: No such file or directory
/usr/lib/gcc/aarch64-alpine-linux-musl/12.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: cannot find -lintl: No such file or directory

@tonimelisma
Copy link
Collaborator

Hey @sonu27 I've never tried it personally.

@g-mero
Copy link

g-mero commented Sep 10, 2023

Same issue.

@moguchev
Copy link

The same issue

@moguchev
Copy link

Here is my Dockerfile:

FROM --platform=$BUILDPLATFORM golang:1.21 as builder

RUN apt-get update && apt-get install -y gcc-aarch64-linux-gnu gcc-x86-64-linux-gnu && apt-get clean
RUN dpkg --add-architecture arm64 && dpkg --add-architecture amd64

ARG TARGETOS TARGETARCH

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY main.go /app/main.go

RUN apt-get update && apt-get install --no-install-recommends -y libvips libvips-dev


RUN --mount=type=cache,target=/root/.cache/go-build \
    --mount=type=cache,target=/go/pkg \
    if [ "${TARGETARCH}" = "arm64" ]; then CC=aarch64-linux-gnu-gcc && CC_FOR_TARGET=gcc-aarch64-linux-gnu; \
    elif [ "${TARGETARCH}" = "amd64" ]; then CC=x86_64-linux-gnu-gcc && CC_FOR_TARGET=gcc-x86-64-linux-gnu; fi && \
    CGO_ENABLED=1 GOOS=linux GOARCH=${TARGETARCH} CC=$CC CC_FOR_TARGET=$CC_FOR_TARGET go build -a --ldflags '-linkmode external -extldflags "-static" -s -w' -o output/main main.go


FROM alpine:latest
WORKDIR /root
COPY --from=builder /app/output/main .
CMD /root/main

BUILDPLATFORM=linux/amd64:

17.10 /usr/lib/gcc-cross/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/bin/ld: cannot find -lvips: No such file or directory
17.10 /usr/lib/gcc-cross/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/bin/ld: cannot find -lgio-2.0: No such file or directory
17.10 /usr/lib/gcc-cross/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/bin/ld: cannot find -lgobject-2.0: No such file or directory
17.10 /usr/lib/gcc-cross/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/bin/ld: cannot find -lglib-2.0: No such file or directory
17.10 collect2: error: ld returned 1 exit status

BUILDPLATFORM=linux/arm64/v8:

18.55 /usr/local/go/pkg/tool/linux_arm64/link: running aarch64-linux-gnu-gcc failed: exit status 1
18.55 /usr/bin/ld: cannot find -lvips: No such file or directory
18.55 /usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/12/../../../aarch64-linux-gnu/libglib-2.0.a(gutils.c.o): in function `g_get_user_database_entry':
18.55 (.text+0x28c): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
18.55 /usr/bin/ld: (.text+0xe8): warning: Using 'getpwnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
18.55 /usr/bin/ld: (.text+0x11c): warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
18.55 collect2: error: ld returned 1 exit status

Without options --ldflags '-linkmode external -extldflags "-static" -s -w' the same errors.
It seems I'm doing something wrong...

@omar391
Copy link

omar391 commented Jan 17, 2024

Did you try with musl libc?

try this one: #376 (comment)

@moguchev
Copy link

Yes. It's seems that static build is impossible (Or some magic should happen). Without static flag everything is fine.

Here is my final Docker file (build is working on MacOS Sonoma 14.1.1 with M1 and also on Ubuntu):

#################################
# STEP 1. Build executable binary
#################################
FROM --platform=linux/amd64 golang:1.21-alpine3.18 AS build

ARG TARGETOS TARGETARCH
ARG BUILDPLATFORM TARGETPLATFORM

RUN echo "I am running on $(uname -m), building for $TARGETPLATFORM, OS=$TARGETOS, ARCH=$TARGETARCH"

# Download gcc (cross-compilers) for CGO
RUN apk update && apk add build-base

# Install required libraries for libvips and deps
RUN apk update && \
    apk add \
    pkgconfig \
	vips-dev

WORKDIR /src

# Download necessary Go modules
COPY go.mod .
COPY go.sum .
RUN CGO_ENABLED=1 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go mod download

# Copy all files
COPY . .

# Build go binary
ENV CC=gcc
ENV GOOS=linux
ENV GOARCH=amd64
ENV CGO_ENABLED=1
RUN go build -v -a -tags vips --ldflags="-s -w -linkmode external" -o /bin/facade -v ./cmd/facade
# CGO_ENABLED=1 GOOS=${TARGETOS} GOARCH=${TARGETARCH} CC=gcc 
# #############################
# # STEP 2. Build a small image
# #############################
FROM --platform=linux/amd64 alpine:3.18.0 AS final

# Add SSL certs
ADD certs/root.crt /etc/ssl/certs/
ADD certs/root.crt ~/.postgresql/
ADD certs/root.crt ~/.opensearch/

# Copy builded libraries from build image
COPY --from=build /bin/facade /facade

RUN apk update && \
    apk add \
    glib \
    libexpat \
    libwebp \
    libwebp-tools \
    libpng \
    libjpeg-turbo \
    vips \
    file 

RUN file /facade 
RUN ldd /facade

EXPOSE 8080
EXPOSE 8082

CMD /facade

@aisnote
Copy link

aisnote commented Mar 30, 2024

same issue on windows native building, it worked on libvips-8.10, but can not worked on 8.14.
Anyone has idea?
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgio-2.0: No such file or directory
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lintl: No such file or directory
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgmodule-2.0: No such file or directory
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lintl: No such file or directory
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lintl: No such file or directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants