大型專案(如 Linux Kernel)程式碼閱讀,Emacs + Cscope 是經典黃金組合:Cscope 建立符號索引,Emacs 提供交互式導航介面,實現函式呼叫追蹤、變數定義跳轉、檔案關聯搜尋。
# Debian/Ubuntu
sudo apt-get install cscope
# RHEL/CentOS/Fedora
sudo yum install cscope
# 或
sudo dnf install cscope
# 官網下載原始碼編譯
wget https://cscope.sourceforge.net/cscope-15.9.tar.gz
tar xzf cscope-15.9.tar.gz
cd cscope-15.9
./configure && make && sudo make install
Cscope 原始碼包含 contrib/xcscope/xcscope.el,複製至 Emacs load-path 目錄(如 ~/.emacs.d/lisp/),在 ~/.emacs 或 ~/.emacs.d/init.el 加入:
(require 'xcscope)
# Linux Kernel 範例:收集 .c .h .S 檔
find . -type f \( -name "*.c" -o -name "*.h" -o -name "*.S" \) > cscope.files
# 通用專案:可依語言調整
find . -type f -name "*.[ch]" > cscope.files
cscope -b -k
-b:批次模式,僅建庫不進入互動介面-k:Kernel 模式,忽略系統標頭檔(/usr/include 等),縮小索引、提升精準度生成 cscope.out、cscope.in.out、cscope.po.out 三個索引檔。
開啟專案內任一源碼檔,Emacs 自動載入同目錄下的 cscope.out。常用鍵綁(前綴 C-c s):
| 快捷鍵 | 功能 |
|---|---|
C-c s C-h | 顯示所有 Cscope 相關快捷鍵幫助 |
C-c s a | 設定索引檔目錄(專案切換時使用) |
C-c s c | 查找**呼叫該函式**的所有位置 |
C-c s g | 查找該符號的**全域定義** |
C-c s f | 查找該符號對應的**檔案** |
C-c s d | 查找該函式**呼叫的函式** |
C-c s e | 搜尋**正則表達式** |
C-c s s | 搜尋**符號** |