ZLib 示例代码编译错误 LNK2019 [英] error LNK2019 for ZLib sample code compiling

查看:36
本文介绍了ZLib 示例代码编译错误 LNK2019的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 vs2010 中创建了 win32 控制台应用程序(没有选择预编译头的选项).我在下面插入了代码.但 *.obj 链接失败.您能否提供有关该错误的更多信息.查了MSDN,还是没看懂.

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]);}

输出:

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 ==========

推荐答案

啊,请原谅我的提问,但您实际上是否在 zlib 的库或目标文件中进行链接(可能是 zlib1.dll,如果您'正在使用最新版本)?

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

到源文件的顶部.

并非所有控制路径"警告是因为您指定两个函数返回一个 int 但实际上并没有返回一个.现在只需将它们更改为返回 void,您可以稍后添加错误检查.

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.

这篇关于ZLib 示例代码编译错误 LNK2019的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆