1

I've been trying to figure out how to statically build a Go file for hours. Multiple dependencies use CGO, one of which binds to the C GLFW UI framework. This is my import line (full source code):

import (
    "io"
    "os"
    "fmt"
    "math"
    "time"
    "sync"
    "os/exec"
    "strings"
    "strconv"
    "runtime"
    "io/ioutil"
    "image/color"
    "crypto/md5"
    "archive/zip"
    "crypto/rand"
    "crypto/sha1"
    "encoding/hex"
    "path/filepath"
    "crypto/sha256"
    "runtime/debug"
    "github.com/pkg/browser"
    "github.com/zeebo/blake3"
    "golang.org/x/crypto/sha3"
    "golang.org/x/crypto/argon2"
    g "github.com/AllenDang/giu"
    di "github.com/sqweek/dialog"
    "golang.org/x/crypto/blake2b"
    "golang.org/x/crypto/blake2s"
    "github.com/atotto/clipboard"
    "github.com/klauspost/reedsolomon"
    ig "github.com/AllenDang/imgui-go"
    "golang.org/x/crypto/chacha20poly1305"
    "github.com/HACKERALERT/Picocypher/monocypher"
)

When I go build -ldflags='-s -w -extldflags=-static' myfile.go, I get this error:

# command-line-arguments
/usr/local/go/pkg/tool/linux_amd64/link: running g++ failed: exit status 1
/usr/bin/ld: cannot find -lgtk-3
/usr/bin/ld: cannot find -lgdk-3
/usr/bin/ld: cannot find -lpangocairo-1.0
/usr/bin/ld: cannot find -lpango-1.0
/usr/bin/ld: cannot find -latk-1.0
/usr/bin/ld: cannot find -lgdk_pixbuf-2.0
/usr/bin/ld: cannot find -lGL
/usr/bin/ld: /tmp/go-link-881090396/000058.o: in function `_glfwInitVulkan':
/home/XXXXXX/go/pkg/mod/github.com/go-gl/glfw/v3.3/glfw@v0.0.0-20210410170116-ea3d685f79fb/glfw/src/vulkan.c:63: warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: cannot find -lGL
collect2: error: ld returned 1 exit status

I have tried the following:

go build -ldflags='-s -w -extldflags=-static' myfile.go
go build -ldflags='-s -w -extldflags="-lm -static"' myfile.go
go build -ldflags='-s -w -extldflags="-lm -lGL -static"' myfile.go
go build -ldflags='-s -w -extldflags="-lgtk-3 -lstdc++ -static"' myfile.go

I'm on Debian 11, and I have gcc make curl git tar wget xz-utils libx11-dev libxcursor-dev libxrandr-dev libxinerama-dev libxi-dev libgl1-mesa-dev libxxf86vm-dev libgtk-3-dev xdg-utils installed. What am I doing wrong, and is there a way to fix it? Thank you in advance.

Edit: Thanks to whoever suggested usr/bin/ld: cannot find -l<nameOfTheLibrary> as a possible duplicate. While it is related to my issue, it's more oriented toward C. Because this question is about Go, I don't think it solves my problem.

  • Does this answer your question? [usr/bin/ld: cannot find -l](https://stackoverflow.com/questions/16710047/usr-bin-ld-cannot-find-lnameofthelibrary) – Hymns For Disco May 28 '21 at 22:13
  • Sort of. I already read that post through before asking this question, but that requires building a C file, and I'm not sure if it will work with Go. I'm generally not very experienced with C. –  May 28 '21 at 22:50
  • 1
    https://github.com/go-gl/glfw/issues/263 – tink May 29 '21 at 00:09
  • 1
    In either case, the source of the error message is from g++ (which in your case is being invoked by `go build`). I believe you must pass this `-L/...` option to ldflags. – Hymns For Disco May 29 '21 at 00:11
  • @tink Thank you for that thread. I think that's the issue. –  May 29 '21 at 01:53
  • 1
    Pleasure. Sorry I couldn't bare better news. – tink May 29 '21 at 02:27

0 Answers0