UNIX再学习 -- 错误和警告
錯(cuò)誤和警告是常會(huì)出現(xiàn)的現(xiàn)象,了解它對(duì)以后解決問題會(huì)很有幫助。下面我們就重點(diǎn)來詳細(xì)介紹它們。
一、錯(cuò)誤
1、回憶錯(cuò)誤
我們之前講解其他內(nèi)容時(shí)有涉及到錯(cuò)誤的部分,下面讓我們來回憶一下:
(1)參看:C語(yǔ)言再學(xué)習(xí) -- C 預(yù)處理器
#error ?字符串 => 表示產(chǎn)生一個(gè)錯(cuò)誤信息
#warning 字符串 => 表示產(chǎn)生一個(gè)警告信息
(2)參看:C語(yǔ)言再學(xué)習(xí) -- 關(guān)鍵字return和exit ()函數(shù)
C語(yǔ)言中通過使用返回來表示是否出錯(cuò),根據(jù)返回值來進(jìn)行具體的錯(cuò)誤處理一般規(guī)則:1)如果返回值類型時(shí)int類型,并且返回的值不可能是負(fù)數(shù)時(shí),則使用返回值-1代表出錯(cuò),其他數(shù)據(jù)表示正常返回。
2)如果返回值類型時(shí)int類型,并且返回的值可能是負(fù)數(shù)時(shí),則需要使用指針取出返回值的數(shù)據(jù),返回值僅僅表示是否出錯(cuò),-1表示出錯(cuò),0表示正常返回。
3)如果返回值類型是指針類型,則返回值NULL代表出錯(cuò)。
4)如果不考慮是否出錯(cuò),返回值類型使用void即可。
(3)參看:C語(yǔ)言再學(xué)習(xí) -- 文件
stderr -- 標(biāo)準(zhǔn)錯(cuò)誤輸出設(shè)備,例如:
fprintf(stderr, "Can't open it!\n");
(4)C語(yǔ)言再學(xué)習(xí) -- EOF、feof函數(shù)、ferror函數(shù)
ferror () 函數(shù)用法。
2、errno 錯(cuò)誤代碼
經(jīng)常在調(diào)用 linux 系統(tǒng) api 的時(shí)候會(huì)出現(xiàn)一些錯(cuò)誤,比方說使用open () write () creat () 之類的函數(shù)有些時(shí)候會(huì)返回 -1,也就是調(diào)用失敗,這個(gè)時(shí)候往往需要知道失敗的原因。UNIX/Linux 為我們提供了外部全局變量 errno,當(dāng)函數(shù)調(diào)用失敗,會(huì)將具體的錯(cuò)誤編號(hào)設(shè)置到 errno ,我們可以通過 errno 來獲取錯(cuò)誤的原因。下面我們來介紹 errno。(1)介紹errno
error 全局變量在 error.h 頭文件定義,extern int errno
在文件 /usr/include/errno.h /* Declare the `errno' variable, unless it's defined as a macro bybits/errno.h. This is the case in GNU, where it is a per-threadvariable. This redeclaration using the macro still works, but itwill be a function declaration without a prototype and may triggera -Wstrict-prototypes warning. */ #ifndef errno extern int errno; #endif 錯(cuò)誤 Exx 的宏定義在 /usr/include/asm-generic 文件夾下面的 ?errno-base.h 和 errno.h,分別定義了 1-34 、35-132 的錯(cuò)誤定義。
查看 /usr/include/asm-generic/errno-base.h #ifndef _ASM_GENERIC_ERRNO_BASE_H #define _ASM_GENERIC_ERRNO_BASE_H#define EPERM 1 /* Operation not permitted */ #define ENOENT 2 /* No such file or directory */ #define ESRCH 3 /* No such process */ #define EINTR 4 /* Interrupted system call */ #define EIO 5 /* I/O error */ #define ENXIO 6 /* No such device or address */ #define E2BIG 7 /* Argument list too long */ #define ENOEXEC 8 /* Exec format error */ #define EBADF 9 /* Bad file number */ #define ECHILD 10 /* No child processes */ #define EAGAIN 11 /* Try again */ #define ENOMEM 12 /* Out of memory */ #define EACCES 13 /* Permission denied */ #define EFAULT 14 /* Bad address */ #define ENOTBLK 15 /* Block device required */ #define EBUSY 16 /* Device or resource busy */ #define EEXIST 17 /* File exists */ #define EXDEV 18 /* Cross-device link */ #define ENODEV 19 /* No such device */ #define ENOTDIR 20 /* Not a directory */ #define EISDIR 21 /* Is a directory */ #define EINVAL 22 /* Invalid argument */ #define ENFILE 23 /* File table overflow */ #define EMFILE 24 /* Too many open files */ #define ENOTTY 25 /* Not a typewriter */ #define ETXTBSY 26 /* Text file busy */ #define EFBIG 27 /* File too large */ #define ENOSPC 28 /* No space left on device */ #define ESPIPE 29 /* Illegal seek */ #define EROFS 30 /* Read-only file system */ #define EMLINK 31 /* Too many links */ #define EPIPE 32 /* Broken pipe */ #define EDOM 33 /* Math argument out of domain of func */ #define ERANGE 34 /* Math result not representable */#endif 查看 /usr/include/asm-generic/errno.h #ifndef _ASM_GENERIC_ERRNO_H #define _ASM_GENERIC_ERRNO_H#include <asm-generic/errno-base.h>#define EDEADLK 35 /* Resource deadlock would occur */ #define ENAMETOOLONG 36 /* File name too long */ #define ENOLCK 37 /* No record locks available */ #define ENOSYS 38 /* Function not implemented */ #define ENOTEMPTY 39 /* Directory not empty */ #define ELOOP 40 /* Too many symbolic links encountered */ #define EWOULDBLOCK EAGAIN /* Operation would block */ #define ENOMSG 42 /* No message of desired type */ #define EIDRM 43 /* Identifier removed */ #define ECHRNG 44 /* Channel number out of range */ #define EL2NSYNC 45 /* Level 2 not synchronized */ #define EL3HLT 46 /* Level 3 halted */ #define EL3RST 47 /* Level 3 reset */ #define ELNRNG 48 /* Link number out of range */ #define EUNATCH 49 /* Protocol driver not attached */ #define ENOCSI 50 /* No CSI structure available */ #define EL2HLT 51 /* Level 2 halted */ #define EBADE 52 /* Invalid exchange */ #define EBADR 53 /* Invalid request descriptor */ #define EXFULL 54 /* Exchange full */ #define ENOANO 55 /* No anode */ #define EBADRQC 56 /* Invalid request code */ #define EBADSLT 57 /* Invalid slot */#define EDEADLOCK EDEADLK#define EBFONT 59 /* Bad font file format */ #define ENOSTR 60 /* Device not a stream */ #define ENODATA 61 /* No data available */ #define ETIME 62 /* Timer expired */ #define ENOSR 63 /* Out of streams resources */ #define ENONET 64 /* Machine is not on the network */ #define ENOPKG 65 /* Package not installed */ #define EREMOTE 66 /* Object is remote */ #define ENOLINK 67 /* Link has been severed */ #define EADV 68 /* Advertise error */ #define ESRMNT 69 /* Srmount error */ #define ECOMM 70 /* Communication error on send */ #define EPROTO 71 /* Protocol error */ #define EMULTIHOP 72 /* Multihop attempted */ #define EDOTDOT 73 /* RFS specific error */ #define EBADMSG 74 /* Not a data message */ #define EOVERFLOW 75 /* Value too large for defined data type */ #define ENOTUNIQ 76 /* Name not unique on network */ #define EBADFD 77 /* File descriptor in bad state */ #define EREMCHG 78 /* Remote address changed */ #define ELIBACC 79 /* Can not access a needed shared library */ #define ELIBBAD 80 /* Accessing a corrupted shared library */ #define ELIBSCN 81 /* .lib section in a.out corrupted */ #define ELIBMAX 82 /* Attempting to link in too many shared libraries */ #define ELIBEXEC 83 /* Cannot exec a shared library directly */ #define EILSEQ 84 /* Illegal byte sequence */ #define ERESTART 85 /* Interrupted system call should be restarted */ #define ESTRPIPE 86 /* Streams pipe error */ #define EUSERS 87 /* Too many users */ #define ENOTSOCK 88 /* Socket operation on non-socket */ #define EDESTADDRREQ 89 /* Destination address required */ #define EMSGSIZE 90 /* Message too long */ #define EPROTOTYPE 91 /* Protocol wrong type for socket */ #define ENOPROTOOPT 92 /* Protocol not available */ #define EPROTONOSUPPORT 93 /* Protocol not supported */ #define ESOCKTNOSUPPORT 94 /* Socket type not supported */ #define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */ #define EPFNOSUPPORT 96 /* Protocol family not supported */ #define EAFNOSUPPORT 97 /* Address family not supported by protocol */ #define EADDRINUSE 98 /* Address already in use */ #define EADDRNOTAVAIL 99 /* Cannot assign requested address */ #define ENETDOWN 100 /* Network is down */ #define ENETUNREACH 101 /* Network is unreachable */ #define ENETRESET 102 /* Network dropped connection because of reset */ #define ECONNABORTED 103 /* Software caused connection abort */ #define ECONNRESET 104 /* Connection reset by peer */ #define ENOBUFS 105 /* No buffer space available */ #define EISCONN 106 /* Transport endpoint is already connected */ #define ENOTCONN 107 /* Transport endpoint is not connected */ #define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */ #define ETOOMANYREFS 109 /* Too many references: cannot splice */ #define ETIMEDOUT 110 /* Connection timed out */ #define ECONNREFUSED 111 /* Connection refused */ #define EHOSTDOWN 112 /* Host is down */ #define EHOSTUNREACH 113 /* No route to host */ #define EALREADY 114 /* Operation already in progress */ #define EINPROGRESS 115 /* Operation now in progress */ #define ESTALE 116 /* Stale NFS file handle */ #define EUCLEAN 117 /* Structure needs cleaning */ #define ENOTNAM 118 /* Not a XENIX named type file */ #define ENAVAIL 119 /* No XENIX semaphores available */ #define EISNAM 120 /* Is a named type file */ #define EREMOTEIO 121 /* Remote I/O error */ #define EDQUOT 122 /* Quota exceeded */#define ENOMEDIUM 123 /* No medium found */ #define EMEDIUMTYPE 124 /* Wrong medium type */ #define ECANCELED 125 /* Operation Canceled */ #define ENOKEY 126 /* Required key not available */ #define EKEYEXPIRED 127 /* Key has expired */ #define EKEYREVOKED 128 /* Key has been revoked */ #define EKEYREJECTED 129 /* Key was rejected by service *//* for robust mutexes */ #define EOWNERDEAD 130 /* Owner died */ #define ENOTRECOVERABLE 131 /* State not recoverable */#define ERFKILL 132 /* Operation not possible due to RF-kill */#define EHWPOISON 133 /* Memory page has hardware error */#endif 還有一些更大的錯(cuò)誤號(hào)是留給內(nèi)核級(jí)別的,如系統(tǒng)調(diào)用等,用戶程序一般是看不見的這些號(hào)的,Ubuntu12.04中/usr/src/linux-headers-3.2.0-23-generic-pae/include/linux/errno.h?
查看 /usr/src/linux-headers-3.2.0-23-generic-pae/include/linux/errno.h #ifndef _LINUX_ERRNO_H #define _LINUX_ERRNO_H#include <asm/errno.h>#ifdef __KERNEL__/** These should never be seen by user programs. To return one of ERESTART** codes, signal_pending() MUST be set. Note that ptrace can observe these* at syscall exit tracing, but they will never be left for the debugged user* process to see.*/ #define ERESTARTSYS 512 #define ERESTARTNOINTR 513 #define ERESTARTNOHAND 514 /* restart if no handler.. */ #define ENOIOCTLCMD 515 /* No ioctl command */ #define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall *//* Defined for the NFSv3 protocol */ #define EBADHANDLE 521 /* Illegal NFS file handle */ #define ENOTSYNC 522 /* Update synchronization mismatch */ #define EBADCOOKIE 523 /* Cookie is stale */ #define ENOTSUPP 524 /* Operation is not supported */ #define ETOOSMALL 525 /* Buffer or request is too small */ #define ESERVERFAULT 526 /* An untranslatable error occurred */ #define EBADTYPE 527 /* Type not supported by server */ #define EJUKEBOX 528 /* Request initiated, but will not complete before timeout */ #define EIOCBQUEUED 529 /* iocb queued, will get completion event */ #define EIOCBRETRY 530 /* iocb queued, will trigger a retry */#endif#endif
(2)errno 轉(zhuǎn)換成 字符串
1)strerror 函數(shù)
#include <string.h>char *strerror(int errnum);
函數(shù)功能:
主要用于將參數(shù)指定的錯(cuò)誤編號(hào)翻譯成對(duì)應(yīng)的錯(cuò)誤信息返回。
#include <stdio.h> #include <errno.h> #include <string.h>extern int errno ;int main () {FILE *fp;// file.txt 不存在fp = fopen("file.txt", "r");if( fp == NULL ) {fprintf(stderr, "錯(cuò)誤碼: %d\n", errno);fprintf(stderr, "對(duì)應(yīng)錯(cuò)誤信息為: %s\n", strerror(errno));}else {fclose(fp);}return(0); } 輸出結(jié)果: 錯(cuò)誤碼: 2 對(duì)應(yīng)錯(cuò)誤信息為: No such file or directory
2)perror 函數(shù) (重點(diǎn))
#include <stdio.h>void perror(const char *s);
函數(shù)功能: 表示將最后一個(gè)錯(cuò)誤信息打印出來,參數(shù) s 不為空時(shí)原樣輸出,后面追加一個(gè)冒號(hào)和空格,再跟著錯(cuò)誤信息,以及換行。#include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h>extern int errno ;int main (void) {FILE *fp;// file.txt 不存在fp = fopen("file.txt", "r"); if( fp == NULL ) { printf("錯(cuò)誤碼 = %d\n",errno); perror ("打開失敗"), exit (-1); } else { fclose(fp); } return(0); } 輸出結(jié)果: 錯(cuò)誤碼 = 2 打開失敗: No such file or directory
3)printf 函數(shù)
使用 printf("%m\n"); ?%m 格式化標(biāo)記打印錯(cuò)誤信息?#include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h>extern int errno ;int main (void) {FILE *fp;// file.txt 不存在fp = fopen("file.txt", "r"); if( fp == NULL ) { printf("錯(cuò)誤碼 = %d\n",errno); printf ("%m\n"); } else { fclose(fp); } return(0); } 輸出結(jié)果: 錯(cuò)誤碼 = 2 No such file or directory(3)不能根據(jù)錯(cuò)誤號(hào)判斷是否出錯(cuò)
雖然所有的錯(cuò)誤號(hào)都不是零,但是因?yàn)楹瘮?shù)執(zhí)行成功的情況下錯(cuò)誤號(hào)全局變量 errno 不會(huì)被修改,所以不能用該變量的值為零或非零,作為出錯(cuò)或沒出錯(cuò)的判斷依據(jù)。例如:#include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h>extern int errno ;int main () {FILE *fp;// file.txt 不存在 fp = fopen("file.txt", "r");FILE *fp_test;// test.txt 存在 fp_test = fopen("test.txt", "r");if(errno) //不能根據(jù) errno 判斷是否出錯(cuò){ fprintf (stderr, "打開失敗\n");}else {fclose(fp_test);}return(0); } 輸出結(jié)果: 打開失敗 上述例子,就可以看出本來不應(yīng)該打印 "打開失敗" 的。 如果非要使用錯(cuò)誤號(hào)判斷是否出錯(cuò)也可以,那你在調(diào)用它之前必須手動(dòng)將errno清零。 #include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h>extern int errno ;int main () {FILE *fp;// file.txt 不存在 fp = fopen("file.txt", "r");errno = 0; //將 errno 清零FILE *fp_test;// test.txt 存在 fp_test = fopen("test.txt", "r");if(errno) //不能根據(jù) errno 判斷是否出錯(cuò){ fprintf (stderr, "打開失敗\n");}else {fclose(fp_test);}return(0); } 正確的做法是,先根據(jù)函數(shù)的返回值判斷是否出錯(cuò),在確定出錯(cuò)的前提下再根據(jù) errno 的值判斷具體出了什么錯(cuò)。 #include <stdio.h> #include <errno.h> #include <string.h>extern int errno ;int main () {FILE *fp;// file.txt 不存在fp = fopen("file.txt", "r");if( fp == NULL ) {fprintf(stderr, "錯(cuò)誤碼: %d\n", errno);fprintf(stderr, "對(duì)應(yīng)錯(cuò)誤信息為: %s\n", strerror(errno));}else {fclose(fp);}return(0); } 輸出結(jié)果: 錯(cuò)誤碼: 2 對(duì)應(yīng)錯(cuò)誤信息為: No such file or directory二、編譯錯(cuò)誤和警告
1、上面提到的 errno 是標(biāo)準(zhǔn)庫(kù)函數(shù)的錯(cuò)誤代碼,現(xiàn)在來看看gcc編譯錯(cuò)誤和警告
參看:C語(yǔ)言再學(xué)習(xí) -- GCC編譯過程(1)讓所有編譯警告都顯示出來,選項(xiàng) -Wall
如下,編輯一段警告的代碼 #include <stdio.h> int main (void) { int i; printf ("\n hello world![i]\n", i); return 0; } root@ubuntu:/home/tarena/project/c_test# gcc -Wall hello.c -o hello hello.c: 在函數(shù)‘main’中: hello.c:6:2: 警告: 提供給格式字符串的實(shí)參太多 [-Wformat-extra-args] hello.c:6:9: 警告: 此函數(shù)中的‘i’在使用前未初始化 [-Wuninitialized](2)將編譯警告轉(zhuǎn)換成錯(cuò)誤的選項(xiàng) -Werror
編譯警告很多時(shí)候會(huì)被我們忽視,在特殊場(chǎng)合我們還是需要重視編譯警告的,如果能把編譯警告變成直接輸出錯(cuò)誤,那我們的重視程度會(huì)提高很多并去解決。#include <stdio.h> int main (void) { int i; printf ("\n hello world![i]\n", i); return 0; } root@ubuntu:/home/tarena/project/c_test# gcc -Wall -Werror hello.c hello.c: 在函數(shù)‘main’中: hello.c:6:2: 錯(cuò)誤: 提供給格式字符串的實(shí)參太多 [-Werror=format-extra-args] cc1: all warnings being treated as errors
(3)警告級(jí)別
如果你覺得警告級(jí)別不夠,可以使用更高的警告級(jí)別。參看:Options to Request or Suppress Warnings
2、C語(yǔ)言編譯錯(cuò)誤及警告對(duì)照表?
參看:c語(yǔ)言的錯(cuò)誤及警告對(duì)照表參看:16種C語(yǔ)言編譯警告(Warning)類型的解決方法
3、將警告,錯(cuò)誤等信息輸出到文件中
參看:將Linux腳本中的正常輸出,警告,錯(cuò)誤等信息輸出到文件中(1)其中標(biāo)準(zhǔn)輸出、標(biāo)準(zhǔn)輸出、標(biāo)準(zhǔn)錯(cuò)誤,可參看:C語(yǔ)言再學(xué)習(xí) -- 文件
C 程序自動(dòng)打開3個(gè)文件。這3個(gè)文件被稱為標(biāo)準(zhǔn)輸入,標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤輸出。默認(rèn)的標(biāo)準(zhǔn)輸入是系統(tǒng)的一般輸入設(shè)備,通常為鍵盤;默認(rèn)的標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤輸出是系統(tǒng)的一般輸出設(shè)備,通常為顯示器,分別得到文件描述符 0, 1, 2.
下面的方法從標(biāo)準(zhǔn)輸入(鍵盤)獲得一個(gè)字符: ?ch = getchar ( );
標(biāo)準(zhǔn)文件指針:
stdio.h文件把3個(gè)文件指針與3個(gè)C 程序自動(dòng)打開的標(biāo)準(zhǔn)文件進(jìn)行了并聯(lián),如下表所示:
標(biāo)準(zhǔn)文件 ? | 文件指針 ? | 一般使用的設(shè)備 ? |
標(biāo)準(zhǔn)輸入 | stdin | 鍵盤 |
標(biāo)準(zhǔn)輸出 | stdout | 顯示器 |
標(biāo)準(zhǔn)錯(cuò)誤 | stderr | 顯示器 |
這些指針都是FILE指針類型,所以可以被用作標(biāo)準(zhǔn)I/O函數(shù)的參數(shù)。
(2)下面以make命令為例來說明,如何把對(duì)應(yīng)的信息,輸出到對(duì)應(yīng)的文件中:
【用法】1)想要把make輸出的全部信息,輸出到某個(gè)文件中,最常見的辦法就是:
make xxx > build_output.txt
此時(shí)默認(rèn)情況是沒有改變2=stderr的輸出方式,還是屏幕,所以,如果有錯(cuò)誤信息,還是可以在屏幕上看到的。
2)只需要把make輸出中的錯(cuò)誤(及警告)信息輸出到文件中ing,可以用:
make xxx 2> build_output.txt
相應(yīng)地,由于1=stdout沒有變,還是屏幕,所以,那些命令執(zhí)行時(shí)候輸出的正常信息,還是會(huì)輸出到屏幕上,你還是可以在屏幕上看到的。
3)只需要把make輸出中的正常(非錯(cuò)誤,非警告)的信息輸出到文件中,可以用:
make xxx 1> build_output.txt
相應(yīng)地,由于2=stderr沒有變,還是屏幕,所以,那些命令執(zhí)行時(shí)候輸出的錯(cuò)誤信息,還是會(huì)輸出到屏幕上,你還是可以在屏幕上看到的。
4)想要把正常輸出信息和錯(cuò)誤信息輸出到分別的文件中,可以用:
make xxx 1> build_output_normal.txt 2>build_output_error.txt
即聯(lián)合使用了1和2,正常信息和錯(cuò)誤信息,都輸出到對(duì)應(yīng)文件中了。
5)所有的信息都輸出到同一個(gè)文件中:
make xxx > build_output_all.txt 2>&1
其中的2>&1表示錯(cuò)誤信息輸出到&1中,而&1,指的是前面的那個(gè)文件:build_output_all.txt 。
注意:上面所有的1,2等數(shù)字,后面緊跟著大于號(hào)'>' ,中間不能有空格。
總結(jié)
以上是生活随笔為你收集整理的UNIX再学习 -- 错误和警告的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 互联网晚报 | 04月05日 星期二 |
- 下一篇: UNIX再学习 -- shell编程