vC编译问题!

xixiaifl 发布于 2011/04/20 12:55
阅读 908
收藏 1
VC

// dd.cpp : Defines the entry point for the console application.
//

#include "StdAfx.h"
#include <stdio.h>
#include "stdlib.h"
#include <dos.h>
#include "windows.h"
/*#ifdef NEEDS_IO_H*/
#include <io.h>
#include <memory.h>
#include <tchar.h>
/*#endif */
#include <sys/types.h>
#include <sys/locking.h>
#include <signal.h>
#include "system.h"
#include "error.h"
#include "md5.h"
#include "zlib.h"
#include "string.h"
// The input buffer needs to be sector aligned when reading from some physical
// devices.  Not all devices enforce this restriction but some do (e.g. firewire drives).
// The SWAB_ALIGN_OFFSET is used for conversions that are no longer supported and
// will throw the input buffer out of alignment.
#define SWAB_ALIGN_OFFSET 0
#define INPUT_FILE 0
#define OUTPUT_FILE 1
#define FALSE 0
#define TRUE 1
#define max(a, b) ((a) > (b) ? (a) : (b))

//#include <WinCred.h> // added 8/17/2004

 

static void write_output __P ((char* _obuf));
char *xmalloc (size_t n);
int safe_read (int desc, unsigned char* ptr, size_t len);
unsigned int full_write (int desc, unsigned char *ptr, size_t len);
int complete_sparse(int fd, size_t len);

 

 

/* Use separate input and output buffers, and combine partial input blocks. */
#define C_TWOBUFS     0x04000
#define C_COMPRESS    0x10000
#define C_DECOMPRESS  0x20000
#define C_NOERROR     0x0400
#define C_BLOCK       0x010
#define C_UNBLOCK     0x020
#define C_SWAB        0x0200
#define C_SYNC        0x02000
/* Current index into `obuf'. */
static size_t oc = 0;

/* The name of the input file, or NULL for the standard input. */
char *input_file = NULL;

/* The input file descriptor. */
int input_fd = -1;
gzFile gzfh = (gzFile)0;

/* Number of full blocks read. */
size_t r_full = 0;

/* The number of bytes in which atomic reads are done. */
size_t input_blocksize = (size_t)-1;

/* The number of bytes in which atomic writes are done. */
size_t output_blocksize = (size_t) -1;

/* Conversion buffer size, in bytes.  0 prevents conversions. */
static size_t conversion_blocksize = 0;

/* Skip this many records of `output_blocksize' bytes before output. */
_off_t seek_record = 0;

/* Copy only this many records.  <0 means no limit. */
size_t max_records = (size_t)-1;

/* Bit vector of conversions to apply. */
int conversions_mask = 0;

/* Skip this many records of `input_blocksize' bytes before input. */
_off_t skip_records = 0;

/* The name of the output file, or NULL for the standard output. */
char *output_file = NULL;

long bCancelled = 0;

/* Number of partial blocks read. */
static size_t r_partial = 0;

/* The output file descriptor. */
int output_fd = -1;

/* Number of partial blocks written. */
static size_t w_partial = 0;

/* Number of full blocks written. */
size_t w_full = 0;

/* If nonzero, filter characters through the translation table.  */
static int translation_needed = 0;

/* If nonnzero, the last char from the previous call to `swab_buffer'
   is saved in `saved_char'.  */
static int char_is_saved = 0;

/* Records truncated by conv=block. */
static size_t r_truncate = 0;

/* Odd char from previous call.  */
static char saved_char;

/* Index into current line, for `conv=block' and `conv=unblock'.  */
static size_t col = 0;

/* Output representation of newline and space characters.
   They change if we're converting to EBCDIC.  */
static char newline_character = '\n';
static char space_character = ' ';

/* Translation table formed by applying successive transformations. */
static unsigned char trans_table[256];

 int o_physical_drive[];
 __int64 _DriveSize;
 bool in_sparse;


 void write_output (unsigned char* obuf)
{
 unsigned int nwritten = full_write (output_fd, obuf, output_blocksize);
 if (nwritten != output_blocksize)
    {
  error (0, errno, _T("%s"), output_file);
  if (nwritten > 0)
   w_partial++;
  exit (1);
    }
 else
  w_full++;
 oc = 0;
}

 


void output_char(unsigned char c, unsigned char* buf)
{
 buf[oc++] = (c);
 if (oc >= output_blocksize)
  write_output(buf);
}

 

 

/* Apply the character-set translations specified by the user
   to the NREAD bytes in BUF.  */

 void translate_buffer (unsigned char *buf, int nread)
{
  register unsigned char *cp;
  register int i;

  for (i = nread, cp = buf; i; i--, cp++)
    *cp = trans_table[*cp];
}


/* Copy NREAD bytes of BUF, doing conv=block
   (pad newline-terminated records to `conversion_blocksize',
   replacing the newline with trailing spaces).  */


 void copy_with_block (unsigned char *buf, size_t nread, unsigned char* obuf)
{
  register size_t i;

  for (i = nread; i; i--, buf++)
    {
      if (*buf == newline_character)
 {
   size_t pending_spaces = max (0, conversion_blocksize - col);
   while (pending_spaces)
     {
       output_char (space_character, obuf);
       --pending_spaces;
     }
   col = 0;
 }
      else
 {
   if (col == conversion_blocksize)
     r_truncate++;
   else if (col < conversion_blocksize)
     output_char (*buf, obuf);
   col++;
 }
    }
}

/* Copy NREAD bytes of BUF, doing conv=unblock
   (replace trailing spaces in `conversion_blocksize'-sized records
   with a newline).  */

 void copy_with_unblock (unsigned char *buf, size_t nread, unsigned char* obuf)
{
  register size_t i;
  register unsigned char c;
  static int pending_spaces = 0;

  for (i = 0; i < nread; i++)
    {
      c = buf[i];

      if (col++ >= conversion_blocksize)
 {
   col = pending_spaces = 0; /* Wipe out any pending spaces.  */
   i--;   /* Push the char back; get it later. */
   output_char (newline_character, obuf);
 }
      else if (c == space_character)
 pending_spaces++;
      else
 {
   /* `c' is the character after a run of spaces that were not
      at the end of the conversion buffer.  Output them.  */
   while (pending_spaces)
     {
       output_char (space_character, obuf);
       --pending_spaces;
     }
   output_char (c, obuf);
 }
    }
}

static __int64 skip_gzread(gzFile fdesc, TCHAR *file, _off_t records, size_t blocksize, unsigned char *buf)
{
 __int64 total_read = 0;

 while (records-- > 0)
 {
   int nread;

   nread = gzread (fdesc, buf, (unsigned int)blocksize);
   if (nread < 0)
     {
   int err;
   error (0, 0, gzerror(fdesc, &err));
      exit (1);
     }
   /* POSIX doesn't say what to do when dd detects it has been
      asked to skip past EOF, so I assume it's non-fatal.
      FIXME: maybe give a warning.  */
   if (nread == 0)
     break;
   total_read += nread;
 }
 return total_read;
}

 void gzskip (gzFile fdesc, TCHAR *file, _off_t records, size_t blocksize, unsigned char *buf)
{
 
 if (gzseek(fdesc, records * blocksize, SEEK_CUR) < 0 &&
  skip_gzread(fdesc, file, records, blocksize, buf) < 0)
 {
  int err;
  error (0, 0, gzerror(fdesc, &err));
  exit (1);
 }
}
static __int64 skip_read(int fdesc, TCHAR *file, _off_t records, size_t blocksize, unsigned char *buf)
{
 __int64 total_read = (__int64)0;

 while (records-- > 0)
 {
   int nread;

   nread = safe_read (fdesc, buf, blocksize);
   if (nread < 0)
     {
       error (0, errno, _T("%s"), file);
       exit (1);
     }
   /* POSIX doesn't say what to do when dd detects it has been
      asked to skip past EOF, so I assume it's non-fatal.
      FIXME: maybe give a warning.  */
   if (nread == 0)
     break;
   total_read += nread;
 }
 return total_read;
}

void skip (int fdesc, TCHAR *file, _off_t records, size_t blocksize, unsigned char *buf)
{
 if (lseek (fdesc, records * blocksize, SEEK_SET) < 0 &&
  skip_read(fdesc, file, records, blocksize, buf) < 0)
 {
  error (0, errno, _T("%s"), file);
  exit (1);
 }
}

 


 void handle_bad_region(__int64 Start, __int64 End, size_t blocksize)
{
 _ftprintf(stderr, _T("File data in the range 0x%08I64x-0x%08I64x could not be read.\n"), Start, End + blocksize);
}

 

 

 unsigned char *swab_buffer (unsigned char *buf, int *nread)
{
  unsigned char *bufstart = buf;
  register unsigned char *cp;
  register int i;

  /* Is a char left from last time?  */
  if (char_is_saved)
    {
      *--bufstart = saved_char;
      (*nread)++;
      char_is_saved = 0;
    }

  if (*nread & 1)
    {
      /* An odd number of chars are in the buffer.  */
      saved_char = bufstart[--*nread];
      char_is_saved = 1;
    }

  /* Do the byte-swapping by moving every second character two
     positions toward the end, working from the end of the buffer
     toward the beginning.  This way we only move half of the data.  */

  cp = bufstart + *nread; /* Start one char past the last.  */
  for (i = *nread / 2; i; i--, cp -= 2)
    *cp = *(cp - 2);

  return ++bufstart;
}

 

 void copy_simple (unsigned char *buf, size_t nread, unsigned char* _obuf)
{
 size_t nfree;   /* Number of unused bytes in `obuf'.  */
 unsigned char *start = buf; /* First uncopied char in BUF.  */
 //printf("copy_simple\n");
 //printf("lbl at copy_simple\n");
 do
    {
  nfree = output_blocksize - oc;
  if (nfree > (size_t)nread)
   nfree = nread;
  
  memcpy ((char *) (_obuf + oc), (char *) start, nfree);
  
  nread -= nfree;  /* Update the number of bytes left to copy. */
  start += nfree;
  oc += (_off_t)nfree;
  if (oc >= output_blocksize)
   write_output (_obuf);
    }
 while (nread > 0);
}

 

 void main(void)               //copy file
{
 __int64 BadStart = 0;
 __int64 BadLast = 0;
 int bInBadRegion = 0;
 unsigned char *bufstart = (unsigned char*)0; /* Input buffer.输入缓冲区 */
 unsigned int count = 0;
 _off_t nread;   /* Bytes read in the current block.读入当前模块的字节 */
 int exit_status = 0;
// unsigned char md5buffer[MD5_SIGNATURE_SIZE];
 unsigned char *ibuf = (unsigned char*) 0;
 unsigned char *obuf = (unsigned char*)0;
// PMD5CTX md5_ctx = (PMD5CTX)0;

 if (max_records == 0)
  exit (exit_status);
    printf("test at copy_file\n");
#ifdef _WIN32
  __try
  {
#endif //_WIN32

   /* Leave at least one extra byte at the beginning and end of `ibuf'
  for conv=swab, but keep the buffer address even.  But some peculiar
  device drivers work only with word-aligned buffers, so leave an
  extra two bytes.  */

 ibuf = (unsigned char *) xmalloc (input_blocksize + (2 * SWAB_ALIGN_OFFSET) + 72);       ////??/
 
 memset ((char *) ibuf, 0, input_blocksize + (2 * SWAB_ALIGN_OFFSET) + 72);

 ibuf += SWAB_ALIGN_OFFSET;

 if ((conversions_mask & C_TWOBUFS) == C_TWOBUFS)
 {
  
  obuf = (unsigned char *) xmalloc (output_blocksize);
 }
 else
 {
  
  obuf = ibuf;
 }


 
 if (skip_records > 0)
 {
  
  if((conversions_mask & C_DECOMPRESS) == C_DECOMPRESS)
   gzskip(gzfh, input_file, skip_records, input_blocksize, ibuf);       //压缩
  else
   skip(input_fd, input_file, skip_records, input_blocksize, ibuf);
 }

 if (seek_record > 0)
 {
  
  if((conversions_mask & C_COMPRESS) == C_COMPRESS) 
   gzskip(gzfh, output_file, skip_records, output_blocksize, obuf);
  else
   skip (output_fd, output_file, seek_record, output_blocksize, obuf);
 }

  _ftprintf(stderr, _T("Copying %s to %s...\n"), input_file, output_file);
  fflush(stderr);

  while (1)
 {
                             // Control handler may not get scheduled without this.
  Sleep(0L);                 //added 8/17/2004以固定的时间间隔中止正在运行的线程

  if(bCancelled)
   break;

  if (max_records >= 0 && r_partial + r_full >= max_records)
   break;

   /* Zero the buffer before reading, so that if we get a read error,
  whatever data we are able to read is followed by zeros.
  This minimizes data loss. *///读之前将缓冲区清零,这样即使出现读错误,也可以将数据丢失减到最小
   if ((conversions_mask & C_SYNC) == C_SYNC &&
    (conversions_mask & C_NOERROR) == C_NOERROR)
  memset ((char *) ibuf, 0, input_blocksize);

 errno=0;

 if(bInBadRegion)
 {
  __int64 pos = -1I64;

  
  if((conversions_mask & C_DECOMPRESS) == C_DECOMPRESS)
   pos = gzseek(gzfh, input_blocksize, SEEK_CUR);
  else
   pos = lseek(input_fd, input_blocksize, SEEK_CUR);
  
  if(pos < 0I64)
  {
   // Might mean that we are at the end of the file.
   // Handle the bad region and break.
   if(bInBadRegion)
   {
    handle_bad_region(BadStart, BadLast, input_blocksize);
    bInBadRegion = FALSE;
   }
   error(0, errno, _T("Failed to seek beyond bad file region at 0x%I64x\n"), BadLast);        
   break;
  }
 }

 if((conversions_mask & C_DECOMPRESS) == C_DECOMPRESS)
  nread = gzread(gzfh, ibuf, (unsigned int)input_blocksize);
 else
  nread = safe_read (input_fd, ibuf, input_blocksize);

 if (nread == 0)
 {
  if(bInBadRegion)
  {
   handle_bad_region(BadStart, BadLast, input_blocksize);
   bInBadRegion = FALSE;
  }
  break;   /* EOF.  */
 }
 else if (nread < 0)
 {
  int err = errno;

  if((conversions_mask & C_DECOMPRESS) == C_DECOMPRESS)
   BadLast = gztell(gzfh);
  else
   BadLast = tell(input_fd);

  if(BadLast == -1I64)
  {
   //Either this is a non-seeking device or some serious error has
   //occurred.
   error(1, errno, _T("Failed to get offset of bad file region\n"));    
   break;
  }
  
  if(o_physical_drive[INPUT_FILE] && BadLast >= (_DriveSize * 1024I64))
  {
   

   error(0, err, _T("read operation returning error for EOF condition at offset 0x%I64x.\n"), BadLast);

   if(bInBadRegion)
   {
    handle_bad_region(BadStart, BadLast, input_blocksize);
    bInBadRegion = FALSE;
   }

   nread = 0;

   break;   /* EOF.  */
  }

  if(!bInBadRegion)
  {
   if((conversions_mask & C_DECOMPRESS) == C_DECOMPRESS)
   {
    error(0, 0, gzerror(gzfh, &err));
   }
   else
   {
    error (0, err, _T("%s (offset 0x%I64x)"), input_file, BadLast);
   }
  }

  if ((conversions_mask & C_NOERROR) == C_NOERROR)
  {
   if(!bInBadRegion)
   {
    BadStart = BadLast;
    bInBadRegion = TRUE;
   }
   
   nread = (off_t) input_blocksize;
   memset (ibuf, 0, input_blocksize);
#ifdef CONVERSIONS_SUPPORTED
   if ((conversions_mask & C_SYNC) == C_SYNC)
   /* Replace the missing input with null bytes and
   proceed normally.  */
    nread = 0;
   else
#endif //CONVERSIONS_SUPPORTED
//    continue;
  }
   else
  {
    /* Write any partial block. */
    exit_status = 2;
    break;
  }
 }
 else if (((size_t)nread) < input_blocksize)
 {
  __int64 pos;

  if((conversions_mask & C_DECOMPRESS) == C_DECOMPRESS)
   pos = gztell(gzfh);
  else
   pos = tell(input_fd);

  error (0, errno, _T("%s (offset 0x%I64x)"), input_file, pos);

  r_partial++;

  if(bInBadRegion)
  {
   handle_bad_region(BadStart, BadLast, input_blocksize);
   bInBadRegion = FALSE;
  }

#if CONVERSIONS_SUPPORTED
  if ((conversions_mask & C_SYNC) == C_SYNC)
  {
    if ((conversions_mask & C_NOERROR) == 0)
   /* If C_NOERROR, we zeroed the block before reading. */
   memset ((char *) (ibuf + nread), 0, input_blocksize - nread);
   nread = (_off_t)input_blocksize;
  }
#endif //CONVERSIONS_SUPPORTED
 }
 else //if(nread == input_blocksize)
 {
  r_full++;

  if(bInBadRegion)
  {
   handle_bad_region(BadStart, BadLast, input_blocksize);
   bInBadRegion = FALSE;
  }
 }

 if(bCancelled)
  break;
 
  if (ibuf == obuf)  /* If not C_TWOBUFS. */
 {
  
   int nwritten = 0;

  
      
  if((conversions_mask & C_COMPRESS) == C_COMPRESS)
   nwritten = gzwrite(gzfh, obuf, nread);
   
  else
  {
   nwritten = full_write (output_fd, obuf, nread);
   //printf("copy_file:while(1):ibuf=obuf\n");
  }

   if (nwritten < 0)
  {
    error (0, errno, _T("%s"), output_file);
    exit (1);
  }
   else if (nread == input_blocksize)
   {
    
  w_full++;
   }
   else
   {
 
  w_partial++;
   }
   continue;
 }

 if(bCancelled)
  break;

 /* Do any translations on the whole buffer at once.  */

 if (translation_needed)
 {
 
  translate_buffer (ibuf, nread);
 }

 if(bCancelled)
  break;

#ifdef CONVERSIONS_SUPPORTED
 if ((conversions_mask & C_SWAB) == C_SWAB)
  bufstart = swab_buffer (ibuf, &nread);
 else
#endif //CONVERSIONS_SUPPORTED

  bufstart = ibuf;

 if(bCancelled)
  break;

#ifdef CONVERSIONS_SUPPORTED 
   
  if ((conversions_mask & C_BLOCK) == C_BLOCK)
  copy_with_block (bufstart, nread, obuf);
   else if ((conversions_mask & C_UNBLOCK) == C_UNBLOCK)
  copy_with_unblock (bufstart, nread, obuf);
   else
#endif //CONVERSIONS_SUPPORTED
  copy_simple (bufstart, nread, obuf);
 }

  /* If we have a char left as a result of conv=swab, output it.  */
  if (char_is_saved)
 {
#ifdef CONVERSIONS_SUPPORTED
   if (conversions_mask & C_BLOCK)
  copy_with_block (&saved_char, 1);
   else if ((conversions_mask & C_UNBLOCK) == C_UNBLOCK)
 copy_with_unblock (&saved_char, 1);
   else
#endif //CONVERSIONS_SUPPORTED

 output_char (saved_char, obuf);
 }

 if(bCancelled)
  __leave;

#ifdef CONVERSIONS_SUPPORTED 
 if ((conversions_mask & C_BLOCK) && col > 0)
 {
    /* If the final input line didn't end with a '\n', pad
   the output block to `conversion_blocksize' chars.  */
    size_t pending_spaces = max (0, conversion_blocksize - col);
    while (pending_spaces)
   {
     output_char (space_character, obuf);
     --pending_spaces;
   }
 }

  if ((conversions_mask & C_UNBLOCK) && col == conversion_blocksize)
 /* Add a final '\n' if there are exactly `conversion_blocksize'
    characters in the final record. */
  output_char (newline_character, obuf);
#endif // CONVERSIONS_SUPPORTED
 if(bCancelled)
  __leave;
 
  /* Write out the last block. */
 if (oc > 0)
 {
  int nwritten;

  if((conversions_mask & C_COMPRESS) == C_COMPRESS)
   nwritten = gzwrite(gzfh, obuf, nread);
  else
   nwritten = full_write (output_fd, obuf, oc);
   if (nwritten > 0)
  w_partial++;
  if (nwritten < 0)
  {
   if((conversions_mask & C_COMPRESS) == C_COMPRESS)
   {
    int err;
    error(0, 0, gzerror(gzfh, &err));
   }
   else
   {
    error (0, errno, _T("%s"), output_file);
   }
   exit (1);
  }
 }
 


 if(in_sparse)
  complete_sparse(output_fd, 0);
#ifdef _WIN32
 }
 __finally
#endif //_WIN32
 {
  if(obuf != ibuf)
   free(obuf);

  free (ibuf - SWAB_ALIGN_OFFSET);


 }
}

 

编译时没有错误,但链接时有如下的错误,请问是怎么回事啊?该如何解决?谢谢!

Linking...
dd.obj : error LNK2001: unresolved external symbol _error
dd.obj : error LNK2001: unresolved external symbol "unsigned int __cdecl full_write(int,unsigned char *,unsigned int)" (?full_write@@YAIHPAEI@Z)
dd.obj : error LNK2001: unresolved external symbol _gzerror
dd.obj : error LNK2001: unresolved external symbol _gzseek
dd.obj : error LNK2001: unresolved external symbol _gzread
dd.obj : error LNK2001: unresolved external symbol "int __cdecl safe_read(int,unsigned char *,unsigned int)" (?safe_read@@YAHHPAEI@Z)
dd.obj : error LNK2001: unresolved external symbol "int __cdecl complete_sparse(int,unsigned int)" (?complete_sparse@@YAHHI@Z)
dd.obj : error LNK2001: unresolved external symbol _gzwrite
dd.obj : error LNK2001: unresolved external symbol "int * o_physical_drive" (?o_physical_drive@@3PAHA)
dd.obj : error LNK2001: unresolved external symbol _gztell
dd.obj : error LNK2001: unresolved external symbol "char * __cdecl xmalloc(unsigned int)" (?xmalloc@@YAPADI@Z)
Debug/dd.exe : fatal error LNK1120: 11 unresolved externals
Error executing link.exe.

dd.exe - 12 error(s), 0 warning(s)

加载中
0
DB_Terrill
DB_Terrill

编译过是指讲法没有问题,连接出现问题大多是出现在库的引用上。

你看看是不是你引用的库中出现重复定义了以上函数或者是变量。

或者是你要使用的库你没有连接上,缺少了相应的库。

0
xixiaifl
xixiaifl

dd.obj : error LNK2001: unresolved external symbol _gzerror
dd.obj : error LNK2001: unresolved external symbol _gzseek
dd.obj : error LNK2001: unresolved external symbol _gzread

dd.obj : error LNK2001: unresolved external symbol _gzwrite

dd.obj : error LNK2001: unresolved external symbol _gztell

这几个错误都是在zconf.h头文件中,难道是这个头文件没链接上吗?


0
xyz555
xyz555

你使用了zlib,如果没有带上zlib的源码编译,那就要带上相关的.lib文件。

0
DB_Terrill
DB_Terrill

引用来自#3楼“吴熙曦”的帖子

dd.obj : error LNK2001: unresolved external symbol _gzerror
dd.obj : error LNK2001: unresolved external symbol _gzseek
dd.obj : error LNK2001: unresolved external symbol _gzread

dd.obj : error LNK2001: unresolved external symbol _gzwrite

dd.obj : error LNK2001: unresolved external symbol _gztell

这几个错误都是在zconf.h头文件中,难道是这个头文件没链接上吗?


头文件是代码的一部分,是在编译的过程中使用的。

你代码中要使用的库我不清楚,4楼说的库你引入下看看吧。你应该是缺少引用的库,或者是你使用的这些变量与其它库的变量重复并且数值不一样。这个错误提示的具体内容不清楚了,前者的可能性大。

0
Lunar_Lin
Lunar_Lin

女的给男的提的问题 一般都很可怕.......

    问题应该基本同前面的兄弟所述了.

    找到ZLIB相应的lib,丢到VC 目录下的lib目录. 在代码中 随便找一个合适的cpp文件(或C)加入

#pragma comment(lib,"***.lib")  即可.

0
dargoner
dargoner

没有指定 lib,贴那么多代码干嘛,直接贴错误信息好了

返回顶部
顶部