2010-05-12 53 views
1

我在vs2010中創建了win32控制檯應用程序(沒有選擇預編譯頭選項)。我插入了下面的代碼。但* .obj鏈接失敗。你能否向我提供有關錯誤的更多信息。我搜索了MSDN,但仍然無法理解它。用於ZLib示例代碼編譯的錯誤LNK2019

#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 *)" ([email protected]@[email protected]) 
1>zlibApp.obj : error LNK2019: unresolved external symbol _gzread referenced in function "int __cdecl decompress_one_file(char *,char *)" ([email protected]@[email protected]) 
1>zlibApp.obj : error LNK2019: unresolved external symbol _gzopen referenced in function "int __cdecl decompress_one_file(char *,char *)" ([email protected]@[email protected]) 
1>zlibApp.obj : error LNK2019: unresolved external symbol _gzwrite referenced in function "int __cdecl compress_one_file(char *,char *)" ([email protected]@[email protected]) 
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 ========== 

回答

2

啊,請原諒我問一下,但你實際上是zlib的(可能zlib1.dll如果您使用了最新版本的庫或對象文件中的鏈接)?

該錯誤通常是由於您錯過了其中包含代碼的實際庫而導致的。事實上,包含頭文件可以讓編譯器知道這些函數存在,但除非將庫與主代碼鏈接在一起,否則鏈接程序將無法找到它們。

您的其他問題很小。忽略那些暗示你使用所謂的「安全」功能的人。這只是微軟試圖鎖定供應商,並對希望編碼到標準的程序員造成傷害。您可以通過添加

#define _CRT_SECURE_NO_DEPRECATE 

到您的源文件頂部來關閉這些警告。

「並非所有控制路徑」警告都是因爲您指定了兩個函數來返回int,但實際上並沒有返回。現在只需更改那些返回void,您可以稍後添加錯誤檢查。

+0

我沒有實際鏈接'zlib1.dll'。我搜索了代碼並從** zlib.net **下載了zlib(1.2.5)。那我該如何解決這個問題。謝謝。 – 2010-05-12 07:49:39

+0

如果您已經從該網站獲得了_source code_,則需要將各個源文件添加到您的項目中,或者您可以下載這個可能更簡單的DLL(http://www.winimage.com/zLibDll/)的index.html)。 – paxdiablo 2010-05-12 07:49:42

+0

按照你的建議,我添加了'zcofig.h'和'zlib.h'到我的項目中,但仍然顯示**錯誤LNK2019 **錯誤。 – 2010-05-12 07:56:00