Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6086720
  • 博文数量: 2759
  • 博客积分: 1021
  • 博客等级: 中士
  • 技术积分: 4091
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-11 14:14
文章分类

全部博文(2759)

文章存档

2019年(1)

2017年(84)

2016年(196)

2015年(204)

2014年(636)

2013年(1176)

2012年(463)

分类: LINUX

2013-03-25 03:08:53

安装cscope:
(1)通过官网下载源码
$tar zxfv cscope-15.5.tar.gz
$cd cscope-15.5
$./configure && make && make install
然后把contrib/xcscope/目录下的cscope-indexer复制到PATH目录比如/usr/local/bin,
再把xcscope.el复制到/usr/share/emacs/site-lisp,
修改/usr/share/emacs/site-lisp/site-start.el,添加(require 'xcscope)。
(2)直接通过工具安装
如果是cygwin中选择安装包,安装之后,xcscope.el被自动放置在/usr/share/emacs/site-lisp;
然后手工创建~/.emacs(没有该文件则手工创建),加入(require 'xcscope),使用xcsope.el的配置。
如果是ubuntu的apt-get安装,还要另外安装cscope-el,不然找不到xcscope.el

sudo apt-get install cscope-el 

xcscope.el 在 /usr/share/emacs/site-lisp


上面工作完成,就可以使用cscope了,如果还想把xcscope.el文件放到系统用户load-path下,可以试试下面方法:
先创建文件夹,mkdir -p ~/.emacs.d/site-lisp/
然后,在emacs的配置文件~/.emacs中添加:
;;LOAD_PATH      
(add-to-list 'load-path' "~/.emacs.d/site-lisp")
在.emacs文件中加入下面的语句:
(require 'xcscope)
如果只希望在打开c/c++文件的时候才加载xcscope,可以加入下面语句:
(add-hook 'c-mode-common-hook '(lambda() (require 'xcscope)))

修改cscope配置
xcscope默认的快捷键都是绑定到C-c s的前缀上面,如果你经常使用xcscope.el,可以自己进行按键绑定,减少击键次数。
具体方法是,在.emacs文件中加入
(define-key global-map [(control f3)]  'cscope-set-initial-directory)
(define-key global-map [(control f4)]  'cscope-unset-initial-directory)
(define-key global-map [(control f5)]  'cscope-find-this-symbol)
(define-key global-map [(control f6)]  'cscope-find-global-definition)
(define-key global-map [(control f7)]  'cscope-find-global-definition-no-prompting)
(define-key global-map [(control f8)]  'cscope-pop-mark)
(define-key global-map [(control f9)]  'cscope-next-symbol)
(define-key global-map [(control f10)] 'cscope-next-file)
(define-key global-map [(control f11)] 'cscope-prev-symbol)
(define-key global-map [(control f12)] 'cscope-prev-file)
(define-key global-map [(meta f9)]     'cscope-display-buffer)
(define-key global-map [(meta f10)]    'cscope-display-buffer-toggle)

cscope索引:
最简单的方法是在源代码目录运行cscope -bR, 
-b 代表build 源码的index 数据库,-R 表示包括子文件夹,
cscope 在源代码的根目录生成index 文件cscope.out。
但是在默认情况下,cscope 在建库时不会引入cpp 文件;如果是cpp 的项目,需要先找到所有的c/cpp 文件,然后再用cscope 建立index,命令如下:

点击(此处)折叠或打开

  1. #找到所有的c,cpp,h 文件
  2. find . -name "*.cpp" -o -name "*.c" -o -name "*.h" > cscope.files
  3. #根据cscope.files 生成database
  4. cscope -b -i cscope.files

不过这么做太麻烦了,所以在cscope 的源代码包中,在contrib/xcscope中有一个文件cscope-indexer,自动完成上述功能,搜索文件夹,并且建cscope 的index。 如果Linux发行版没有把这个文件打到cscope的包中,可以下载源代码包,然后把这个文件拷贝到/bin 下面,就可以用了。

利用cscope-indexer脚本生成文件列表和数据库:
cscope-indexer -r
-r表示递归检索子目录,文件列表和数据库的默认文件名分别为cscope.files和cscope.out,可以使用-i,-f参数进行修改。

cscope-indexer可以根据自己的需要修改,例如这个网上的实例:用quilt 管理patch,会在源代码目录生成/.pc/目录, 里面是拷贝的c 文件,不希望cscope-indexer 去搜索这些文件,可以多加一个规则,虑除.pc 下所有文件

点击(此处)折叠或打开

  1. @@ -138,7 +138,7 @@
  2. fi
  3. ) | \
  4. egrep -i '\.([chly](xx|pp)*|cc|hh)$' | \
  5. - sed -e '/\/CVS\//d' -e '/\/RCS\//d' -e 's/^\.\///' | \
  6. + sed -e '/\/CVS\//d' -e '/\/RCS\//d' -e '/\/\.pc\//d' -e 's/^\.\///'| \
优化查找速度:
索引建好之后,如果发现查函数定义之类的操作很慢,可以进行一些优化:
(1)Cscope默认每次查找时更新cscope.out,可以关闭该选项以提高查找速度。
在~/.emacs文件中加入(setq cscope-do-not-update-database t)
(2)通过创建反向索引加速查找。
调用Cscope时,使用-q参数。具体做法是修改cscope-indexer脚本,将cscope -b -i $LIST_FILE -f $DATABASE_FILE
替换为cscope -q -b -i $LIST_FILE -f $DATABASE_FILE
然后删除老的文件列表和数据库,重新执行cscope-indexer,发现多生成了两个文件cscope.in.out和cscope.po.out。

上述工作完成之后,重启emacs,就可以使用cscope了。

cscope的快捷方式:
(1)基本方式,参见xcscope.el的注释部分:

;; * Keybindings:

;;

;; All keybindings use the "C-c s" prefix, but are usable only while

;; editing a source file, or in the cscope results buffer:

;;

;;      C-c s s         Find symbol.

;;      C-c s d         Find global definition.

;;      C-c s g         Find global definition (alternate binding).

;;      C-c s G         Find global definition without prompting.

;;      C-c s c         Find functions calling a function.

;;      C-c s C         Find called functions (list functions called

;;                      from a function).

;;      C-c s t         Find text string.

;;      C-c s e         Find egrep pattern.

;;      C-c s f         Find a file.

;;      C-c s i         Find files #including a file.

;;

;; These pertain to navigation through the search results:

;;

;;      C-c s b         Display *cscope* buffer.

;;      C-c s B         Auto display *cscope* buffer toggle.

;;      C-c s n         Next symbol.

;;      C-c s N         Next file.

;;      C-c s p         Previous symbol.

;;      C-c s P         Previous file.

;;      C-c s u         Pop mark.

;;

;; These pertain to setting and unsetting the variable,

;; `cscope-initial-directory', (location searched for the cscope database

;;  directory):

;;

;;      C-c s a         Set initial directory.

;;      C-c s A         Unset initial directory.

;;

;; These pertain to cscope database maintenance:

;;

;;      C-c s L         Create list of files to index.

;;      C-c s I         Create list and index.

;;      C-c s E         Edit list of files to index.

;;      C-c s W         Locate this buffer's cscope directory

;;                      ("W" --> "where").

;;      C-c s S         Locate this buffer's cscope directory.

;;                      (alternate binding: "S" --> "show").

;;      C-c s T         Locate this buffer's cscope directory.

;;                      (alternate binding: "T" --> "tell").

;;      C-c s D         Dired this buffer's directory.



(2)看代码经常要跳转,可以使用register来存储标签

C-x r SPC 编号  设置bookmark

C-x r j   编号  跳转到bookmark

也可以通过完整的通用的命令输入方式:
M-x point-to-register
M-x jump-to-register
输入过程中可以用tab补全,看到所有命令。



阅读(647) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~