为什么加了zlib库 还是提示compress()未定义

qiake110 2015-10-22 09:19:19
代码:
// testzlib.cpp 简单测试 zlib 的压缩功能
#include<stdio.h>
#include<zlib.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int err;
Byte compr[200], uncompr[200]; // big enough
uLong comprLen, uncomprLen;
const char* hello = "12345678901234567890123456789012345678901234567890";

uLong len = strlen(hello) + 1;
comprLen = sizeof(compr) / sizeof(compr[0]);

err =compress(compr, &comprLen, (const Bytef*)hello, len);

if (err != Z_OK) {
//cerr << "compess error: " << err << '\n';
printf("error:compress error!\n");
exit(1);
}
//cout << "orignal size: " << len<< " , compressed size : " << comprLen << '\n';
printf("orignal size: %d compressed size: %d \n",len,comprLen);

strcpy((char*)uncompr, "garbage");

err = uncompress(uncompr, &uncomprLen, compr, comprLen);
if (err != Z_OK) {
// cerr << "uncompess error: " << err << '\n';
printf("error:uncompress error!\n");
exit(1);
}
//cout << "orignal size: " << len << " , uncompressed size : " << uncomprLen << '\n';
printf("orignal size:%d uncopressed size :%d \n",len,uncomprLen);
if (strcmp((char*)uncompr, hello)) {
// cerr << "BAD uncompress!!!\n";
printf("error:bad uncompress!");
exit(1);
}
else {
// cout << "uncompress() succeed: \n" << (char *)uncom
printf("uncompress() suceed :%s\n",(char*)uncompr);
}
}
提示错误:
1.c:(.text+0x75): undefined reference to `compress'
1.c:(.text+0xf2): undefined reference to `uncompress'

但是我的/usr/linclude/目录下有zlib库 ,求解啊—。—
...全文
385 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiake110 2015-10-24
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
-lxxxx 指示链接xxxxlib.a
这个就是-lz /usr/include/zlib.h才能成功
赵4老师 2015-10-23
  • 打赏
  • 举报
回复
-lxxxx 指示链接xxxxlib.a
fefe82 2015-10-23
  • 打赏
  • 举报
回复
引用 2 楼 qiake110 的回复:
[quote=引用 1 楼 fefe82 的回复:] 链接时加 -lz 。 你没有链接 zlib 的库。
还真是,太感谢了! 不过求教一下 为什么zlib.h已经在/usr/include/目录下了 还要链接zlib库呢 gcc 默认的链接库 不就是/usr/include/目录吗 [/quote] C 从源代码到程序要经历两个大的步骤,一个是编译(compile),一个是链接(link)。 .h 在编译的时候使用,告诉编译器 compress 是一个函数。(这个由 #include 指定) 库文件在链接的时候使用,提供了 compress 函数的(已经编译好的)具体实现。(这个需要由命令行指定) 库文件不是 .h ,一般为 .a (Linux, 静态库)或 .so (Linux,动态库)
qiake110 2015-10-22
  • 打赏
  • 举报
回复
引用 1 楼 fefe82 的回复:
链接时加 -lz 。 你没有链接 zlib 的库。
还真是,太感谢了! 不过求教一下 为什么zlib.h已经在/usr/include/目录下了 还要链接zlib库呢 gcc 默认的链接库 不就是/usr/include/目录吗
fefe82 2015-10-22
  • 打赏
  • 举报
回复
链接时加 -lz 。 你没有链接 zlib 的库。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧