我正在尝试编译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$
如何正确编译文件?
答案 0 :(得分:3)
您收到链接器错误,因为您没有告诉编译器链接包含您已使用过的函数的库。编译在 Ubuntu 上使用zlib
的简单程序的常用方法是:
gcc -o foo foo.c -lz