在 D 应用程序中链接到 Linux 下的 zlib 时如何解决此错误?

How can I resolve this error when linking to zlib under Linux in a D application?

我主要在 Windows 上工作,所以我对 Linux 下不太常见的问题很陌生。

这是我在 dub 尝试 link 我的应用程序时遇到的错误:

/usr/bin/ld: .dub/obj/pixelperfectengine_pixelperfecteditor.o: undefined reference to symbol 'inflateEnd'
//lib/x86_64-linux-gnu/libz.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Error: /usr/bin/gcc failed with status: 1
/usr/bin/ldc2 failed with exit code 1.

我有一个图像处理库作为依赖项,它是应用程序所必需的,并且它(显然)将 zlib 用于 *.png compression/decompression。我已经为 Ubuntu 安装了 zlib1g-dev,但没有解决我的问题,并且在 Windows.

下编译完全相同的代码没有任何问题

/lib/x86_64-linux-gnu/libz.so.1: error adding symbols: DSO missing from command line

此错误告诉您必须将 -lz 添加到 命令行 。我不知道 dub 是什么,但是您必须以某种方式说服它在它构造的 link 命令中添加 -lz

-lz 添加到您的 dub 文件中的 lflags 可能就足够了。

如前所述,您需要在 zlib 中 link。

我建议通过 "libs" 数组这样做(请参阅此页面:https://dub.pm/package-format-json.html)。使用 libs 优于 lflags 的优点是 libs 将尝试使用 pkg-config,这是获取 linker / C 编译标志的通用方法[++] 个库 POSIX。它适用于 Linux 和 Mac OSX。如果未找到 pkg-config,dub 将默认执行 lflags 首先执行的操作。

这是我自己项目中的一个示例:https://github.com/Geod24/libsodiumd/blob/9b397645e2fc3ca502acb58e1b4631d3faf094e2/dub.json