tags:

views:

117

answers:

1

Hello.

I created win32 console application in vs2010 (without select the option of precompiled header). And I inserted the code below. but *.obj link failed. Could you provide me more information about the error. I searched MSDN, but still can't understand it.

#include <stdio.h>
#include "zlib.h"

// Demonstration of zlib utility functions

unsigned long file_size(char *filename)
{
   FILE *pFile = fopen(filename, "rb");
    fseek (pFile, 0, SEEK_END);
    unsigned long size = ftell(pFile);
    fclose (pFile);
    return size;
 }

 int decompress_one_file(char *infilename, char *outfilename)
 {
    gzFile infile = gzopen(infilename, "rb");
    FILE *outfile = fopen(outfilename, "wb");
    if (!infile || !outfile) return -1;

    char buffer[128];
    int num_read = 0;
    while ((num_read = gzread(infile, buffer, sizeof(buffer))) > 0) {
       fwrite(buffer, 1, num_read, outfile);
       }

    gzclose(infile);
    fclose(outfile);
 }

 int compress_one_file(char *infilename, char *outfilename)
 {
    FILE *infile = fopen(infilename, "rb");
    gzFile outfile = gzopen(outfilename, "wb");
    if (!infile || !outfile) return -1;

    char inbuffer[128];
   int num_read = 0;
    unsigned long total_read = 0, total_wrote = 0;
    while ((num_read = fread(inbuffer, 1, sizeof(inbuffer), infile)) > 0) {
       total_read += num_read;
       gzwrite(outfile, inbuffer, num_read);
    }
    fclose(infile);
    gzclose(outfile);

    printf("Read %ld bytes, Wrote %ld bytes, Compression factor %4.2f%%\n",
       total_read, file_size(outfilename),
       (1.0-file_size(outfilename)*1.0/total_read)*100.0);
 }        
 int main(int argc, char **argv)
 {
    compress_one_file(argv[1],argv[2]);
    decompress_one_file(argv[2],argv[3]);}

Output:

1>------ Build started: Project: zlibApp, Configuration: Debug Win32 ------
1>  zlibApp.cpp
1>d:\learning\cpp\cppvs2010\zlibapp\zlibapp\zlibapp.cpp(15): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files\microsoft visual studio 10.0\vc\include\stdio.h(234) : see declaration of 'fopen'
1>d:\learning\cpp\cppvs2010\zlibapp\zlibapp\zlibapp.cpp(25): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files\microsoft visual studio 10.0\vc\include\stdio.h(234) : see declaration of 'fopen'
1>d:\learning\cpp\cppvs2010\zlibapp\zlibapp\zlibapp.cpp(40): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files\microsoft visual studio 10.0\vc\include\stdio.h(234) : see declaration of 'fopen'
1>d:\learning\cpp\cppvs2010\zlibapp\zlibapp\zlibapp.cpp(36): warning C4715: 'decompress_one_file' : not all control paths return a value
1>d:\learning\cpp\cppvs2010\zlibapp\zlibapp\zlibapp.cpp(57): warning C4715: 'compress_one_file' : not all control paths return a value
1>zlibApp.obj : error LNK2019: unresolved external symbol _gzclose referenced in function "int __cdecl decompress_one_file(char *,char *)" (?decompress_one_file@@YAHPAD0@Z)
1>zlibApp.obj : error LNK2019: unresolved external symbol _gzread referenced in function "int __cdecl decompress_one_file(char *,char *)" (?decompress_one_file@@YAHPAD0@Z)
1>zlibApp.obj : error LNK2019: unresolved external symbol _gzopen referenced in function "int __cdecl decompress_one_file(char *,char *)" (?decompress_one_file@@YAHPAD0@Z)
1>zlibApp.obj : error LNK2019: unresolved external symbol _gzwrite referenced in function "int __cdecl compress_one_file(char *,char *)" (?compress_one_file@@YAHPAD0@Z)
1>D:\learning\cpp\cppVS2010\zlibApp\Debug\zlibApp.exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
+1  A: 

Ah, pardon me for asking but are you actually linking in the library or object file for zlib (probably zlib1.dll if you're using an up-to-date version)?

That error is normally caused by the fact the you're missing the actual libraries with the code in it. The fact that you include the header files lets the compiler know that those functions exist but, unless you link the libraries along with your main code, the linker won't be able to find them.

Your other problems are minor. Ignore the ones suggesting that you use the so called "safe" functions. That's just Microsoft attempting some vendor lock-in and does a disservice to programmers who want to code to the standard. You can shut these warnings up by adding

#define _CRT_SECURE_NO_DEPRECATE

to the top of your source file.

The "not all control paths" warnings are because you specify your two functions to return an int but then don't actually return one. Just change those to return a void for now, you can add error checking later.

paxdiablo
@paxdiablo, I didn't link `zlib1.dll` acturally. I googled the code and download zlib (1.2.5) from **zlib.net**. Then how can I solve the issue. Thanks.
Nano HE
If you've got the _source code_ from that site, you'll need to add the individual source files to your project, or you can download the DLL which is probably easier (http://www.winimage.com/zLibDll/index.html).
paxdiablo
Followed you suggeston, I added `zcofig.h` and `zlib.h` to my project, but still show **error LNK2019** error.
Nano HE
I would try `zlib125.dll` which you provided above.
Nano HE
@Nano, if you're compiling the source, you need to add the C files to your project as well as including the H files. But I still suggest the best bet is to link against the DLL.
paxdiablo