linux下怎么使用zlib库(主要是不会使用库)

haihai05 2009-07-22 10:10:30
我下了zlib,解压放在了linux服务器下,自己编了个程序
#include <iostream>
#include <vector>
#include "zlib.h"
using namespace std;
int main()
{
const unsigned char strSrc[] = "jasddsfsfaffafasd";
unsigned char buff[1024] = {0}, strDst[1024] = {0};
unsigned long srcLen = sizeof(strSrc), bufLen = sizeof(buff), dstLen = sizeof(strDst);
//压缩
compress(buff, &bufLen, strSrc, srcLen);
//解压缩
uncompress(strDst, &dstLen, buff, bufLen);
cout << strDst << endl;
return 0;
}
但是程序编译出错
undefined reference to `compress'
undefined reference to `uncompress'

linux下不会使用库,所以出错了,哪位能帮我详细的说下linux下怎么使用库的。
...全文
5270 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuelijun922 2009-07-27
  • 打赏
  • 举报
回复 1
-lz
把lib文件拷到bin下
billow_zhang 2009-07-24
  • 打赏
  • 举报
回复
zlib好像没有.zip文件的解压缩功能.
.zip文件不仅需要压缩和解压缩,还有多个文件的打包和解包处理.

.zip文件的解析问题,可以参考: http://www.info-zip.org/
once_and_again 2009-07-23
  • 打赏
  • 举报
回复

看 其中 的函数。
haihai05 2009-07-23
  • 打赏
  • 举报
回复
还有个问题,zlib能对.zip压缩包解析吗?麻烦给个简单的例子吧!!ths
billow_zhang 2009-07-23
  • 打赏
  • 举报
回复
一般来说,库函数会提供头文件和库目标码.
头文件里含有的是库函数的的定义、数据结构的定义和常量及宏的定义.用于你在调用库函数的时候使用。例如,在zlib.h里面就有下面的定义:

int compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);

所以,当你的程序调用compress,并且在程序开始inlcude zlib.h,编译程序就会检查你的调用于头文件的定义是否相同:主要是参数类型、个数、返回值的检查。
但是在 zlib.h里面,并没有compress的实现的代码。这个函数是存放在compress.o里面的,并且被打包到libz.a的库里面。所以,你在链接的时候,需要使用-l的选项,告诉链接程序,需要链接libz.a的库。也就是使用-lz的选项。

所以,头文件是用于包含在你的源程序中编译使用的。
而库的目标码是用于你链接的时候是用的。
两者都不能缺少。

goodluckyxl 2009-07-23
  • 打赏
  • 举报
回复
找不到函数体

把库连接上去
win74 2009-07-23
  • 打赏
  • 举报
回复
编译已没错,
undefined reference to `compress'
undefined reference to `uncompress'
是链接时的报错,就是没找到库。
linux下链接库用l加库名,注意如:链接libz.a,只需写lz,即前置lib和后置.a不必写
liuwg9999 2009-07-23
  • 打赏
  • 举报
回复
第二个编译是指编译你自己的程序
liuwg9999 2009-07-23
  • 打赏
  • 举报
回复
你把zlib包解压后要编译一下,它会生成一个zlib.so之类的库文件,
编译的时候引用这个库文件就可以#include"zlib.h"了,
就像$ gcc -o z z.c -lz
z就是生成的可执行文件,z.c就是你的代码,-lz就是引用zlib.so了

[Quote=引用 4 楼 haihai05 的回复:]
换个问法,写的那个简单的程序为什么会报那样的错呢?不是说给例子。主要是想明白怎么样在linux下使用像zlib包这样的东东的。是直接解压后放在linux下就可以#include"zlib.h"
给自己的程序服务了吗?如果是但是上面那个为什么不对呢?引用 1 楼 showman 的回复:
zlib包不是有个example.c吗?

一个极简单的例程:

$ cat z.c

#include <stdio.h>
...
[/Quote]
steptodream 2009-07-23
  • 打赏
  • 举报
回复
友情帮顶
Wolf0403 2009-07-23
  • 打赏
  • 举报
回复
zlib 的话,你的 libz.so 在哪里?假设在 /home/user/libz.so,那么编译时候指定

gcc my.c -o my -L/home/user -lz
haihai05 2009-07-23
  • 打赏
  • 举报
回复
谁用过这个zziplib包没?能简单的说下这个的用法吗?
haihai05 2009-07-23
  • 打赏
  • 举报
回复
好像其中没有函数支持.zip包的解压呢?[Quote=引用 16 楼 once_and_again 的回复:]
看 其中 的函数。
[/Quote]
haihai05 2009-07-22
  • 打赏
  • 举报
回复
刚看了,/usr/include下有zlib.h
[Quote=引用 7 楼 darkchampion 的回复:]
你的zlib.h放在哪了?
如果不在/usr/include下,编译时还要指定头文件的路径
gcc -I/yourpath -lz
[/Quote]
DarkChampion 2009-07-22
  • 打赏
  • 举报
回复
你的zlib.h放在哪了?
如果不在/usr/include下,编译时还要指定头文件的路径
gcc -I/yourpath -lz
ShowMan 2009-07-22
  • 打赏
  • 举报
回复
也是对的,只是编译的时候需要link zlib 库, linux下用 -lz
haihai05 2009-07-22
  • 打赏
  • 举报
回复
麻烦大家详细给点解答下,给下相关的资料再好不过了。本人感接触linux下的编程。谢谢大家了。
haihai05 2009-07-22
  • 打赏
  • 举报
回复
换个问法,写的那个简单的程序为什么会报那样的错呢?不是说给例子。主要是想明白怎么样在linux下使用像zlib包这样的东东的。是直接解压后放在linux下就可以#include"zlib.h"
给自己的程序服务了吗?如果是但是上面那个为什么不对呢?
[Quote=引用 1 楼 showman 的回复:]
zlib包不是有个example.c吗?

一个极简单的例程:

$ cat z.c

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <zlib.h>


static void gzip(const char *infile, const char *outfile)

{

  char buf[512];

  FILE *in;

  gzFile out;

  int len;


  if ((in = fopen(infile, "rb")) == NULL) {

    printf("Can't open input file %s
", infile);

    exit(1);

  }


  if ((out = gzopen(outfile, "wb")) == NULL) {

    printf("Can't open output file %s
", outfile);

    exit(1);

  }


  while ((len = fread(buf, 1, sizeof(buf), in)) > 0)

    gzwrite(out, buf, len);


  fclose(in);

  gzclose(out);

}


static void gunzip(const char *infile, const char *outfile)

{

  char buf[512];

  gzFile in;

  FILE *out;

  int len;


  if ((in = gzopen(infile, "rb")) == NULL) {

    printf("Can't open input file %s
", infile);

    exit(1);

  }


  if ((out = fopen(outfile, "wb")) == NULL) {

    printf("Can't open output file %s
", outfile);

    exit(1);

  }


  while ((len = gzread(in, buf, sizeof(buf))) > 0)

    fwrite(buf, 1, len, out);


  gzclose(in);

  fclose(out);

}


static void usage(char *bin)

{

  printf("usage: %s [-d] FILE
"

"  -d  decompress
"

"
", bin);

  exit(1);

}


int main(int ac, char *av[])

{

  char *infile, outfile[256];

  int compress = 1;


  if (ac < 2 || ac > 3)

    usage(av[0]);


  if (ac == 3) {

    if (strcmp(av[1], "-d"))

      usage(av[0]);

    compress = 0;

    infile = av[2];

    strcpy(outfile, infile);

    *strstr(outfile, ".gz") = '';

  }

  else {

    infile = av[1];

    sprintf(outfile, "%s.gz", infile);

  }


  if (compress)

    gzip(infile, outfile);

  else

    gunzip(infile, outfile);


  return 0;

}

$ gcc -o z z.c -lz

$ ./z z.c

$ ./z -d z.c.gz

[/Quote]
ShowMan 2009-07-22
  • 打赏
  • 举报
回复
然也,就是gcc -lz
DarkChampion 2009-07-22
  • 打赏
  • 举报
回复
是不是编译没有引入库 -lz
加载更多回复(1)

23,215

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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