如何修复对 `nettle_sha256_digest` 的未定义引用?

How do I fix an undefined reference to `nettle_sha256_digest`?

TL;DR

我正在尝试构建一个使用此依赖项的 go 项目:https://github.com/mqu/openldap,它又在外部链接 lldap 和 llber 库,后者又使用 lgnutls,lgnutls 使用 lnettle,这就是我'我卡住了。

go build 生成一长串未定义的引用,构建失败。这是一个示例:

/usr/lib/x86_64-linux-gnu/libgnutls.a(sha-x86-ssse3.o): In function `_ctx_init':
(.text+0x468): undefined reference to `nettle_sha256_digest'
/usr/lib/x86_64-linux-gnu/libgnutls.a(sha-x86-ssse3.o): In function `_ctx_init':
(.text+0x476): undefined reference to `nettle_sha224_init'
/usr/lib/x86_64-linux-gnu/libgnutls.a(sha-x86-ssse3.o): In function `_ctx_init':
(.text+0x494): undefined reference to `nettle_sha256_init'
/usr/lib/x86_64-linux-gnu/libgnutls.a(sha-x86-ssse3.o): In function `_ctx_init':
(.text+0x4b8): undefined reference to `nettle_sha256_digest'
/usr/lib/x86_64-linux-gnu/libgnutls.a(sha-x86-ssse3.o): In function `_ctx_init':
(.text+0x4c6): undefined reference to `nettle_sha256_init'
/usr/lib/x86_64-linux-gnu/libgnutls.a(sha-x86-ssse3.o):(.data.rel.ro+0x18): undefined reference to `nettle_sha256_init'
/usr/lib/x86_64-linux-gnu/libgnutls.a(sha-x86-ssse3.o):(.data.rel.ro+0x28): undefined reference to `nettle_sha256_digest'
/usr/lib/x86_64-linux-gnu/libgnutls.a(sha-x86-ssse3.o):(.data.rel.ro+0x58): undefined reference to `nettle_sha224_init'
/usr/lib/x86_64-linux-gnu/libgnutls.a(sha-x86-ssse3.o):(.data.rel.ro+0x68): undefined reference to `nettle_sha224_digest'
/usr/lib/x86_64-linux-gnu/libgnutls.a(sha-x86-ssse3.o):(.data.rel.ro+0x98): undefined reference to `nettle_sha1_init'
/usr/lib/x86_64-linux-gnu/libgnutls.a(sha-x86-ssse3.o):(.data.rel.ro+0xa8): undefined reference to `nettle_sha1_digest'

我的 go build 命令:

CC=/usr/local/musl/bin/musl-gcc \
    GOOS=linux go build \
    -o /bin/activedirectory \
    -ldflags '-linkmode external -extldflags "-static -L/usr/lib/x86_64-linux-gnu -lnettle -lp11 -lsasl2 -lgnutls -ltasn1"'

我尝试通过安装 libnettle4nettle-devlibghc-nettle-devnettle-bin 来解决这个问题。我确保在 ldflags 中包含 -lnettle。运气不好。

更多上下文

openldap 库正在链接代码中的 ldaplber 库:

package openldap

/*

#define LDAP_DEPRECATED 1
#include <stdlib.h>
#include <ldap.h>

static inline char* to_charptr(const void* s) { return (char*)s; }
static inline LDAPControl** to_ldapctrlptr(const void* s) {
    return (LDAPControl**) s;
}
*/
// #cgo CFLAGS: -DLDAP_DEPRECATED=1
// #cgo linux CFLAGS: -DLINUX=1
// #cgo LDFLAGS: -lldap -llber

这要求我安装并在 ldflags 中包含您在我的构建命令中看到的所有库。

简而言之,依赖链是这样的:

lldap -> lgnutls -> lnettle。

我添加了 lgnutls 并解决了我的 gnutls 依赖问题,但我无法解决我的 nettle 依赖问题。

我的问题

我在尝试修复这些荨麻依赖性问题时做错了什么?

加分题

是否有解决这些 ld 链接器依赖项的最佳实践?现在我的流程是这样的:

  1. 运行 构建
  2. 注意 "undefined reference" 错误
  3. 通过谷歌搜索错误或查看引用的名称(例如 nettle_sha1_digest = nettle package)找出缺少哪个包
  4. 安装那个包
  5. 重复

我想我想知道是否有可以为我安装所有依赖项的灵丹妙药? :)

从外观上看,您以错误的顺序链接了依赖库(有关更多上下文,请参阅 Why does the order in which libraries are linked sometimes cause errors in GCC?)。

对于静态链接库,链接器会尝试按照指定库的顺序解析符号。如果依赖的符号出现为未定义,则链接器会查看它是否已在指定的后续库中定义。由于在您的构建调用中指定了 -lnettle before -lgnutls gnutls 库无法解析它需要的符号荨麻

这意味着(至少)您必须将对 -lnettle 的引用移到 -lgnutls 之后。不确定这是否会解决您的所有问题,因为我不熟悉您列出的所有链接依赖项,但它至少应该解决您当前的错误 运行 go build.