LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2005, 04:17 AM   #1
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Rep: Reputation: 15
zlib.h problem


I'm trying to use the zlib library and it seems like it is included and found (when I compile the c file using the zlib.h, there's no error). Never the less, when I link my program I get a undefined reference, to the function I use i zlib (compress2).

Code:
//gzip_interface.c

#include <string.h>
#include <stdlib.h>
#include <linux/zlib.h>
#include "gzip_interface.h"

int compress_buf(char *dst, char *src, unsigned short int level )
{
	int size = strlen(src);
	int return_size = 0;
	dst = malloc(size);
	return_size = compress2(dst, size, src, size, level);
	return 0;
	
}

//gzip_interface.h

#ifndef GZIP_INTERFACE
#define GZIP_INTERFACE

int compress_buf(char *dst, char *src, unsigned short int level);
#endif
How can this be? I've search for zlib, and it is located in /usr/include and also in /usr/include/linux. I've tried including it like this: #include <zlib.h>, but it's the same problem. I'm using Anjuta (on ProMepis 2005.b04), and I've tried adding the library manually, so it links with -lzlib option.. that didn't work either (Second Q: How do you know when it need to be included manually when linking? .. pthread needs to : -lpthread ??).
Any help will be greatly appriciated.

Regards jnusa

Last edited by jnusa; 01-13-2005 at 04:19 AM.
 
Old 01-13-2005, 06:40 AM   #2
__J
Senior Member
 
Registered: Dec 2004
Distribution: Slackware, ROCK
Posts: 1,973

Rep: Reputation: 46
I believe to manually link zlib its:

-lz
 
Old 01-13-2005, 06:52 AM   #3
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
Yeah.. found out after 1½h . Thanks for the reply anyway _.|
 
Old 01-13-2005, 08:37 PM   #4
Inunu
LQ Newbie
 
Registered: Jan 2005
Location: NZ
Distribution: Customized MDK 9
Posts: 13

Rep: Reputation: 0
Hi Jnusa.

You need to manually link using '-lz' as __J said. For any shared library in your /usr/lib say libxxx-yyy.so.* you can always do -lxxx-yyy, just the section between "lib" and ".so.whatever version". E.g. for libglib-2.0.so.0.400.8, use -lglib-2.0.

The other thing is that the destination size of zlib needs to be a pointer to int, so that the actually byte count filled into destination after (un)compression can be passed to its caller. In the sample you seem to pass the value directly into compress2 (the 2nd parameter).

Code:
#include <stdio.h>
#include <zlib.h>

int main(int argc, char *argv[]){
        char *str="This is a string of test for libz compression.";
        char bufferc[1024];
        char bufferu[1024];
        unsigned long sizec=1024, sizeu=1024;
        int retc=0, retu=0;

        retc=compress2(bufferc, &sizec, str, strlen(str), 9); /* compress str to bufferc */
        bufferc[sizec]='\0';
        retu=uncompress(bufferu, &sizeu, bufferc, sizec); /* uncompress bufferc to bufferu */
        bufferu[sizeu]='\0';
        printf("orig=%s\ncompressed(%d,%d)=%s\nuncompressed(%d,%d)=%s\n",
                str, retc, sizec, bufferc, retu, sizeu, bufferu);

        return 0;
}
linux/tmp$ gcc -o gztest gztest.c -lz
linux/tmp$ ./gztest
orig=This is a string of test for libz compression.
compressed(0,52)=x\uffff
\u572e,V
uncompressed(0,46)=This is a string of test for libz compression.
 
Old 01-14-2005, 12:47 AM   #5
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
OK, appriciate it!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
zlib error jurp5 Linux From Scratch 2 12-21-2005 05:33 PM
zlib m2azer Linux - Newbie 1 09-16-2005 04:07 PM
zlib ?! D3javu Linux - Newbie 1 04-05-2005 05:39 AM
whats zlib? madd matt Linux - Software 2 06-08-2004 06:06 PM
Can't Find zlib rebdi Linux - Newbie 1 09-04-2003 09:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration