2011-05-10 113 views
0

可能重複:
c programming problemC程序問題

我寫了一個代碼計算文件的校驗和。但是當我編譯它時,它顯示出我無法解決的問題。我使用Ubuntu 10.04作爲我的操作系統。

我計算校驗碼是這樣的:

#include <stdio.h> 
#include <stdlib.h> 

unsigned checksum(void *buffer, size_t len, unsigned int seed) 
{ 
     unsigned char *buf = (unsigned char *)buffer; 
     size_t i; 

     for (i = 0; i < len; ++i) 
      seed += (unsigned int)(*buf++); 
     return seed; 
} 


int main() 
{ 
     FILE *fp; 
     size_t len; 
     char buf[4096], file[] = "/home/manish.yadav/filename.c"; 

     if (NULL == (fp = fopen(file, "rb"))) 
     { 
      printf("Unable to open %s for reading\n", file); 
      return -1; 
     } 
     len = fread(buf, sizeof(char), sizeof(buf), fp); 
     printf("%zd bytes read\n", len); 
     printf("The checksum of %s is %u\n", file, checksum(buf, len, 0)); 

     return 0 ; 
} 

我的名字checksum.c保存它,用下面的命令編譯它:

gcc checksum.c 

我得到了以下信息:

/tmp/ccatkZlp.o:(.eh_frame+0x12): 未定義的參考 `__gxx_personality_v0' collect2:LD 返回1退出狀態

現在有誰能夠告訴我,我在這個節目做錯了什麼?這些錯誤是什麼以及他們爲什麼來?

請幫我我完全卡住了。

回答

1

在文件的頂部,#include <stdio.h>前右側添加

#ifdef __cplusplus 
#error Compile with a C compiler 
#endif