如何修复对inflate/deflate函数的未定义引用?

8

我正在尝试编译由zlib提供的示例现有代码,但它在第一步就出现错误:

nikhil@nikhil-Vostro-3500:~/zlib-1.2.8/examples$ gcc -o zpipe -g zpipe.c 
/tmp/ccVZzqsb.o: In function `def':
/home/nikhil/zlib-1.2.8/examples/zpipe.c:32: undefined reference to `deflateInit_'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:40: undefined reference to `deflateEnd'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:51: undefined reference to `deflate'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:55: undefined reference to `deflateEnd'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:66: undefined reference to `deflateEnd'
/tmp/ccVZzqsb.o: In function `inf':
/home/nikhil/zlib-1.2.8/examples/zpipe.c:90: undefined reference to `inflateInit_'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:98: undefined reference to `inflateEnd'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:109: undefined reference to `inflate'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:116: undefined reference to `inflateEnd'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:121: undefined reference to `inflateEnd'
/home/nikhil/zlib-1.2.8/examples/zpipe.c:130: undefined reference to `inflateEnd'
collect2: error: ld returned 1 exit status
nikhil@nikhil-Vostro-3500:~/zlib-1.2.8/examples$ 

我该如何正确编译这个文件?


2
你需要告诉编译器在命令行中链接zlib。请查看man gcc中的-l-L标志。 - OmnipotentEntity
1
或者仅在该示例目录中运行“make”。zlib1g和zlib1g-dev是Ubuntu软件包,它们为您提供zlib。 - Dirk Eddelbuettel
1
如果它找到了zlib.h,那么可以合理地假设已经安装了zlib1g-dev - Emmet
@Emmet 谢谢。这很清晰,对于安装来自pecl或源的grpc时出现的“zlib.h:没有那个文件或目录”等其他问题也有帮助:https://github.com/grpc/grpc/issues/12597 - site80443
1个回答

14

出现链接器错误是因为您没有告诉编译器链接包含您使用的函数的库。在 Ubuntu 上编译使用 zlib 的简单程序的通常方法是:

gcc -o foo foo.c -lz

一些澄清,参考文件不在tmp中。gcc在编译过程中创建的临时对象文件位于/tmp中,这是一个链接器错误。 - OmnipotentEntity

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接