c++下zlib的编译问题
- C/C++ code
#include <cstring>#include <cstdlib>#include <iostream>#include "zlib.h" using namespace std; int main(){ int err; Byte compr[200], uncompr[200]; // big enough uLong comprLen, uncomprLen; const char* hello = "12345678901234567890123456789012345678901234567890"; uLong len = strlen(hello) + 1; comprLen = sizeof(compr) / sizeof(compr[0]); err = compress(compr, &comprLen, (const Bytef*)hello, len); if (err != Z_OK) { cerr << "compess error: " << err << '/n'; exit(1); } cout << "orignal size: " << len << " , compressed size : " << comprLen << '/n'; strcpy((char*)uncompr, "garbage"); err = uncompress(uncompr, &uncomprLen, compr, comprLen); if (err != Z_OK) { cerr << "uncompess error: " << err << '/n'; exit(1); } cout << "orignal size: " << len << " , uncompressed size : " << uncomprLen << '/n'; if (strcmp((char*)uncompr, hello)) { cerr << "BAD uncompress!!!/n"; exit(1); } else { cout << "uncompress() succeed: /n" << (char *)uncompr; } return 0;}想用zlib制作压缩工具,这个代码是网上找的。
此CPP文件与zlib.h以及zconf.h在同一目录下,win32 console application工程,编译通过,运行出错:
--------------------Configuration: big - Win32 Debug--------------------
Linking...
big.obj : error LNK2001: unresolved external symbol _uncompress
big.obj : error LNK2001: unresolved external symbol _compress
Debug/big.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
big.exe - 3 error(s), 0 warning(s)
请问如何解决?
[解决办法]
compress这些定义在哪?
如果头文件里面只有声明的话,找找有没有lib文件,用1L的方法链入lib库
[解决办法]
1)请先确认您下的是完整zlib源程序还是zlib的库;
2)如果是完整的源程序,除example.cpp、minigzip等几个演示程序外将所有源码加到工程文件(具体哪些文件记不得了)
3)如果是zlib库,请确认相关的zlib.lib是否被加入到工程文件;运行时将相应zlib.dll放在合适位置(现在还没到运行的时候)
4)因为zlib是c文件,LZ的是CPP文件,请确认zlib.h中是否有这么一句:(CPP和C的命名方式不一样,所以编译后直接调用会找不到)
#ifdef __cplusplus
extern "C" {
#endif
5)最后实在搞不定,可以将zlib的所有.c文件改成.cpp(必须是完整源码,不推荐,至少是不规范做法)
我用的是zlib的1.2.2版本和zlib 1.0.4版本,没有问题
[解决办法]
[解决办法]
加一个编译选项:
-D__cplusplus
动态库使用zlib没问题,主要是你没加载好动态库。