12 Replies - 3370 Views - Last Post: 01 September 2009 - 07:16 PM Rate Topic: -----

#1 nathanpc   User is offline

  • SCIENCE!
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Error When Using zlib

Post icon  Posted 31 August 2009 - 11:32 AM

Hello,
I'm learning C++ and i was trying to use the zlib, but i'm getting some errors when trying to compile my little project. Here is the code:
#include <string>
#include <stdexcept>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <zlib.h>
using namespace std;

std::string compress_string(const std::string& str,
							int compressionlevel = Z_BEST_COMPRESSION)
{
	z_stream zs;						// z_stream is zlib's control structure
	memset(&zs, 0, sizeof(zs));

	if (deflateInit(&zs, compressionlevel) != Z_OK)
		throw(std::runtime_error("deflateInit failed while compressing."));

	zs.next_in = (Bytef*)str.data();
	zs.avail_in = str.size();		   // set the z_stream's input

	int ret;
	char outbuffer[32768];
	std::string outstring;

	// retrieve the compressed bytes blockwise
	do {
		zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
		zs.avail_out = sizeof(outbuffer);

		ret = deflate(&zs, Z_FINISH);

		if (outstring.size() < zs.total_out) {
			// append the block to the output string
			outstring.append(outbuffer,
							 zs.total_out - outstring.size());
		}
	} while (ret == Z_OK);

	deflateEnd(&zs);

	if (ret != Z_STREAM_END) {		  // an error occurred that was not EOF
		std::ostringstream oss;
		oss << "Exception during zlib compression: (" << ret << ") " << zs.msg;
		throw(std::runtime_error(oss.str()));
	}

	return outstring;
}

int main(int argc, char* argv[])
{
	std::string allinput;

	while (std::cin.good())	 // read all input from cin
	{
		char inbuffer[32768];
		std::cin.read(inbuffer, sizeof(inbuffer));
		allinput.append(inbuffer, std::cin.gcount());
	}
		std::string cstr = compress_string( allinput );

		std::cerr << "Deflated data: "
				  << allinput.size() << " -> " << cstr.size()
				  << " (" << std::setprecision(1) << std::fixed
				  << ( (1.0 - (float)cstr.size() / (float)allinput.size()) * 100.0)
				  << "% saved).\n";

		std::cout << cstr;
}

And my compiling log you can found it here: http://nathanpc.pastebin.com/f8c0e7c2

Thanks,
Nathan Paulino Campos

Is This A Good Question/Topic? 0
  • +

Replies To: Error When Using zlib

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Error When Using zlib

Posted 31 August 2009 - 12:30 PM

where did you place zlib.h? are you including that directory in your compiler's includes?

and just to nip farther problems -- do you have the library included as well (needed for linking portion).
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Error When Using zlib

Posted 31 August 2009 - 01:54 PM

The errors are strictly because he is not linking zlib. You need -lzlib when compiling. And if libzlib.a is in a non-standard location, you should add its path to the command line with -L/path/to/lib first.
Was This Post Helpful? 0
  • +
  • -

#4 nathanpc   User is offline

  • SCIENCE!
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Re: Error When Using zlib

Posted 31 August 2009 - 02:31 PM

But i don't know where the libs are, because i've installed then using ./configure, make then sudo make install, compiling it from the sources.

Thanks!

If i put #include "zlib.h" the errors are the same.


*mod edit: icode == il.... [il]something[/il] = something
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Error When Using zlib

Posted 31 August 2009 - 03:40 PM

Like you told earlier in this thread, as well as in this thread (which is rude, BTW), you need to LINK. THE. LIBRARY.

Perhaps reading the zlib FAQ entry #14 would be helpful as well.
Was This Post Helpful? 0
  • +
  • -

#6 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: Error When Using zlib

Posted 31 August 2009 - 04:26 PM

As JackOfAllTrades said you need to link the "zdll.lib" to the project and that should fix everything
Was This Post Helpful? 0
  • +
  • -

#7 nathanpc   User is offline

  • SCIENCE!
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Re: Error When Using zlib

Posted 31 August 2009 - 04:54 PM

Hello Jack,
As you can see, the thread that you put the link here is a thread that i have started, see in my signature, it's my user in DaniWeb and i can't compile it, i do what the FAQ say when i installed zlib, a long time ago.

Thanks,
Nathan Paulino Campos
Was This Post Helpful? 0
  • +
  • -

#8 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: Error When Using zlib

Posted 31 August 2009 - 11:00 PM

Well go to google, search for how to link a .lib to "x compiler" under Linux and you should be fine.

I ran your code and got the same errors after i linked the zdll.lib it worked nicely
Was This Post Helpful? 0
  • +
  • -

#9 nathanpc   User is offline

  • SCIENCE!
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Re: Error When Using zlib

Posted 01 September 2009 - 09:44 AM

Only one more thing: If i do the command like this using g++:
comp.cpp -o comp -lzlib
, i'm getting this errors:
/usr/bin/ld: cannot find -lzlib
collect2: ld returned 1 exit status


Thanks!
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Error When Using zlib

Posted 01 September 2009 - 01:31 PM

Did you read the link I posted to the FAQ, entry 14?
Was This Post Helpful? 0
  • +
  • -

#11 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: Error When Using zlib

Posted 01 September 2009 - 03:16 PM

Maybe this could help

http://homepages.gac...tation/g++.html
Was This Post Helpful? 0
  • +
  • -

#12 nathanpc   User is offline

  • SCIENCE!
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

Re: Error When Using zlib

Posted 01 September 2009 - 04:54 PM

I've readed it and i'm goint to see your link poncho4all!
Thanks!
Was This Post Helpful? 0
  • +
  • -

#13 Guest_c.user*


Reputation:

Re: Error When Using zlib

Posted 01 September 2009 - 07:16 PM

Quote

/usr/bin/ld: cannot find -lzlib


I tryed cc -lz t.c -o t instead

[[email protected] tmp]$ cc -lz t.c -o t
[[email protected] tmp]$ ./t
1.2.3
[[email protected] tmp]$


Was This Post Helpful? 0

Page 1 of 1