libiconv編譯出錯解決

使用iconv命令將文檔的編碼進行轉換即可。

iconv默認情況下,是沒有被安裝的,下面簡單介紹下iconv的安裝過程:
1. 下載
http://www.gnu.org/software/libiconv/#TOCdownloading

2. 安裝:
下載完成後,切換到下載目錄先進行解壓:
[java] view plain copy
  1. $tar -xzvf libiconv-1.14.tar.gz  

然後進入解壓後的文件中

[java] view plain copy
  1. $cd libiconv-1.14_2  
查看其中的README文件,我們可以看到安裝步驟:(當然,如果您熟悉源碼的安裝,這步完全可以省略^-^)
[java] view plain copy
  1. $ ./configure --prefix=/usr/local  
  2. $ make  
  3. $ make install  
3. 命令學習
該工具安裝完成後,肯定要先了解下這個命令的用法吧,這個沒什麼可說的:
[java] view plain copy
  1. $iconv --help  
我們會看到下面的內容:
[java] view plain copy
  1. Usage: iconv [OPTION...] [FILE...]  
  2. Convert encoding of given files from one encoding to another.  
  3.   
  4.  Input/Output format specification:  
  5.   -f, --from-code=NAME       encoding of original text  
  6.   -t, --to-code=NAME         encoding for output  
  7.   
  8.  Information:  
  9.   -l, --list                 list all known coded character sets  
  10.   
  11.  Output control:  
  12.   -c                         omit invalid characters from output  
  13.   -o, --output=FILE          output file  
  14.   -s, --silent               suppress warnings  
  15.       --verbose              print progress information  
  16.   
  17.   -?, --help                 Give this help list  
  18.       --usage                Give a short usage message  
  19.   -V, --version              Print program version  
  20.   
  21. Mandatory or optional arguments to long options are also mandatory or optional  
  22. for any corresponding short options.  
說的很明白,就是按照下面的格式進行轉換:
iconv -f 原編碼 -t 目標編碼 要轉換的文件

4. 編碼轉換:
學會了編碼的轉化,我們就舉了例子示範一下:
[java] view plain copy
  1. $iconv -f gbk -t utf8 test.txt  
[java] view plain copy
  1. <span style="font-family:宋體; font-size:14px">執行下面的命令:</span>  
[java] view plain copy
  1. $iconv -f gbk -t utf8 test.txt > test.convert.txt  
此時我們打開這個test.convert.txt文件就會發現,原來的中文顯示正常了^-^

注意:
如果不出意外的話,上面的安裝步驟可沒有那麼順利,在make的時候,會提示下面的錯誤:
[java] view plain copy
  1. n file included from progname.c:26:0:  
  2. ./stdio.h:1010:1: error: ‘gets’ undeclared here (not in a function)  
  3.  _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");  
  4.  ^  
  5. make[2]: *** [progname.o] Error 1  
  6. make[2]: Leaving directory `/home/freeman/Downloads/libiconv-1.14_2/srclib'  
  7. make[1]: *** [all] Error 2  
  8. make[1]: Leaving directory `/home/freeman/Downloads/libiconv-1.14_2/srclib'  
  9. make: *** [all] Error 2  
這個這個軟件本身存在的一個Bug,通過Google,發現一個解決該問題的補丁,內容如下:
[java] view plain copy
  1. --- srclib/stdio.in.h.orig      2011-08-07 16:42:06.000000000 +0300  
  2. +++ srclib/stdio.in.h   2013-01-10 15:53:03.000000000 +0200  
  3. @@ -695,7 +695,9 @@  
  4.  /* It is very rare that the developer ever has full control of stdin, 
  5.     so any use of gets warrants an unconditional warning.  Assume it is 
  6.     always declared, since it is required by C89.  */  
  7. -_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");  
  8. +#if defined(__GLIBC__) && !defined(__UCLIBC__) && !__GLIBC_PREREQ(216)  
  9. + _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");  
  10. +#endif  
  11.  #endif  
PS:內容中的"+"表示新增的內容,"-"表示刪除的內容!

那我們只要進行如下操作即可解決這個問題:
1. 切換到srclib目錄下:
[java] view plain copy
  1. $cd srclib  

2. 修改stdio.in.h文件:

[java] view plain copy
  1. $gedit stdio.in.h  
通過搜索,定位到_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");這一行,然後在這一行的前後加上條件編譯即可,修改後的內容如下:
[java] view plain copy
  1. #if defined(__GLIBC__) && !defined(__UCLIBC__) && !__GLIBC_PREREQ(216)  
  2.         _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");  
  3. #endif  
3. 保存退出,然後再進行make, make install便可順利安裝^-^
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章