ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.