雖然一直在寫Java,看了些Java類庫源碼之後,對一直感興趣的C庫源代碼也想探索一下。倒騰了一下,在windows下終于解決了如何快速定位自己想看的C函數源碼檔案的方法,也有一點小收獲,終結如下:
1、這裡說的C庫源代碼是GNU glibc,網址:
http://ftp.gnu.org/gnu/glibc/
2、平時調用的C函數,在源碼中是以單獨檔案的形式存在。例如,printf函數路徑
..\glibc-2.20\stdio-common\printf.c
代碼如下:
#include <libioP.h>
#include <stdarg.h>
#include <stdio.h>
#undef printf
/* Write formatted output to stdout from the format string FORMAT. */
/* VARARGS1 */
int
__printf (const char *format, ...)
{
va_list arg;
int done;
va_start (arg, format);
done = vfprintf (stdout, format, arg);
va_end (arg);
return done;
}
#undef _IO_printf
ldbl_strong_alias (__printf, printf);
/* This is for libg++. */
ldbl_strong_alias (__printf, _IO_printf);
這裡需要說明的是源碼中的printf函數前面有一個下劃線,這是為了執行時友善被彙編語言調用。
3、在windows下快速定位C函數源碼檔案方法:
dir C:\Users\...(glibc源檔案存放路徑)\glibc-2.20 /s /b | find "printf.c"
執行結果如下(片段):
C:\Users\zhoujw\Desktop\glibc-2.20\glibc-2.20\stdio-common\printf.c
C:\Users\zhoujw\Desktop\glibc-2.20\glibc-2.20\stdio-common\reg-printf.c
C:\Users\zhoujw\Desktop\glibc-2.20\glibc-2.20\stdio-common\snprintf.c
C:\Users\zhoujw\Desktop\glibc-2.20\glibc-2.20\stdio-common\sprintf.c
C:\Users\zhoujw\Desktop\glibc-2.20\glibc-2.20\stdio-common\test-vfprintf.c
在Linux shell下同樣有find指令。
4、聽說windows下看源代碼用source insight,正在學習,有經驗者還請多指教。在Linux下采用sublime text和ctags配合,閱讀C源碼非常友善。