[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 820
  • Last Modified:

using zlib

hi all,
I am trying to use this zlib library  (in ubuntu) . However, I can't solve the header file. Could you please help me? tks a lot.
sample.c
0
BeginToLearn
Asked:
BeginToLearn
  • 13
  • 9
1 Solution
 
jkrCommented:
Take a look at http://packages.ubuntu.com/search?arch=i386&mode=exactfilename&searchon=contents&keywords=zlib.h - you should have zlib1g-dev installed and be able to find that file as '/usr/include/zlib.h'. Therefore,

#include <zlib.h>

should be fine. If you don't have that package installed, use

$ sudo aptitude update && sudo aptitude install zlib1g-dev
0
 
BeginToLearnAuthor Commented:
after i add
  #include <zlib.h>
 
sample.c:2: fatal error: zlib.h: No such file or directory
compilation terminated.

So it mean i need to install package. Before installation,  I just wonder what if i run my code in other system, do i have to install package again ? tks
0
 
jkrCommented:
Usually not, if you link statically to the library. Yet that should not be a problem at all.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BeginToLearnAuthor Commented:
i try to your command
$ sudo aptitude update && sudo aptitude install zlib1g-dev

it say "command not found"


0
 
BeginToLearnAuthor Commented:
i try this
 sudo apt-get install zlib1g-dev

it is installing now.
0
 
BeginToLearnAuthor Commented:
after installation,
ubuntu@ubuntu:~/program$ g++ -o sample sample.c
/tmp/ccVAewBW.o: In function `decompress_one_file(char*, char*)':
sample.c:(.text+0x8d): undefined reference to `gzopen'
sample.c:(.text+0x125): undefined reference to `gzread'
sample.c:(.text+0x147): undefined reference to `gzclose'
/tmp/ccVAewBW.o: In function `compress_one_file(char*, char*)':
sample.c:(.text+0x1c0): undefined reference to `gzopen'
sample.c:(.text+0x234): undefined reference to `gzwrite'
sample.c:(.text+0x28c): undefined reference to `gzclose'
collect2: ld returned 1 exit status
0
 
jkrCommented:
That's the missing library, you need to specfy it when compiling:

~/program$ g++ -o sample sample.c -lzlib
0
 
BeginToLearnAuthor Commented:
g++ -o sample sample.c -lz
remove compile errors
0
 
jkrCommented:
Just nitpicking - linker errors ;o)
0
 
BeginToLearnAuthor Commented:
what is nitpick?
0
 
jkrCommented:
A synonym for 'pedantic'.
0
 
BeginToLearnAuthor Commented:
when i used your command
ubuntu@ubuntu:~/program$ g++ -o sample sample.c -lzlib
/usr/bin/ld: cannot find -lzlib
collect2: ld returned 1 exit status

I really want to know why?

BTW, could you please show me what to include in sample.c in order to avoid installing package? tks a lot.
0
 
jkrCommented:
That was just an mistake on my side, I was assuming the library was called 'libzlib', apparently it's just 'libz'.

Actually, that should be pretty complete, your program should be able to run on other machines now. Try

nm myapp | grep gzopen
0
 
BeginToLearnAuthor Commented:
As long as it can work on other machines, i am happy.
after i try
nm myapp | grep gzopen

nm: 'myapp': No such file
ubuntu@ubuntu:~/program$
0
 
jkrCommented:
No 'nm' on Ubuntu? Sigh.... Well, anyway, it should run, since 'zlib1g-dev' is a development package. However, be aware that it should only work on machines that run the same Ubuntu version, if not, glibc might differ.
0
 
BeginToLearnAuthor Commented:
I will keep it in mind while demon it. tks jkr
0
 
jkrCommented:
BTW, as a side note - you can get your app on more systems than just one using '-static-libgcc -Xlinker -static'. The executable will increase in size a lot, but that's the price you have to pay ;o)
0
 
BeginToLearnAuthor Commented:
I gonna check it in 2 hrs later
0
 
BeginToLearnAuthor Commented:
i just wonder where to use
  -static-libgcc -Xlinker -static
tks
0
 
jkrCommented:
Well, just like

$ g++ -o sample sample.c -lz -static-libgcc -Xlinker -static
0
 
BeginToLearnAuthor Commented:
tks jkr
0
 
BeginToLearnAuthor Commented:
hi jkr,
Could you please take a look at this question

http://www.experts-exchange.com/Programming/Languages/CPP/Q_26989548.html#a35507798

It is really weird regarding to the decompress. thanks a lot.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 13
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now