We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

using zlib

BeginToLearn
BeginToLearn asked
on
Medium Priority
837 Views
Last Modified: 2012-05-11
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
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

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
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Usually not, if you link statically to the library. Yet that should not be a problem at all.

Author

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

it say "command not found"


Author

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

it is installing now.

Author

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
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
That's the missing library, you need to specfy it when compiling:

~/program$ g++ -o sample sample.c -lzlib

Author

Commented:
g++ -o sample sample.c -lz
remove compile errors
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Just nitpicking - linker errors ;o)

Author

Commented:
what is nitpick?
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
A synonym for 'pedantic'.

Author

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.
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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

Author

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$
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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.

Author

Commented:
I will keep it in mind while demon it. tks jkr
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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)

Author

Commented:
I gonna check it in 2 hrs later

Author

Commented:
i just wonder where to use
  -static-libgcc -Xlinker -static
tks
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Well, just like

$ g++ -o sample sample.c -lz -static-libgcc -Xlinker -static

Author

Commented:
tks jkr

Author

Commented:
hi jkr,
Could you please take a look at this question

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

It is really weird regarding to the decompress. thanks a lot.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.