如何解决如何静态构建使用 CGO 的 Go 文件?
数小时以来,我一直试图弄清楚如何静态构建 Go 文件。多个依赖项使用 CGO,其中之一绑定到 C GLFW UI 框架。这是我的导入行 (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"
)
当我 go build -ldflags='-s -w -extldflags=-static' myfile.go
时,我收到此错误:
# 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
我尝试了以下方法:
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
我使用的是 Debian 11,并且安装了 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
。我做错了什么,有没有办法解决?提前致谢。
编辑:感谢建议 usr/bin/ld: cannot find -l<nameOfTheLibrary> 作为可能重复的人。虽然它与我的问题有关,但它更面向 C。因为这个问题是关于 Go 的,我认为它不能解决我的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。