用emacs 阅读 c/c++ 代码

在emacs编程中有以下需求

  • 从调用一个函数的地方跳转到函数的定义的地方
  • 或是反过来从函数定义的地方列出所有调用这个函数的地方

实现办法

需要安装以下软件

  • gnu global(阅读源代码的工具)官网
  • emacs的gtags插件
  • 启动emacs后,输入下面的命令,来启动gtags的功能
M-x ggtags-mode
gnu global安装
apt-get install global

emacs的gtags插件安装

M-x list-packages

找到ggtags后,鼠标点击,再点安装

使用方法

1,在存放代码的根目录执行:

$ gtags

执行后,会发现在生成了3个文件, GPATH GRTAGS GTAGS

2,启动emacs

3,启动emacs后,输入下面的命令,来启动gtags的功能

M-x ggtags-mode

4,使用下面的函数就可以实现,查看代码的各种需求了。

ggtags-find-tag-dwim

   Find a tag by context.

   If point is at a definition tag, find references, and vice versa.
   If point is at a line that matches ``ggtags-include-pattern``, find
   the include file instead.

   To force finding a definition tag, call it with a prefix (``C-u``).

ggtags-find-tag-mouse

   Like ``ggtags-find-tag-dwim`` but suitable for binding to mouse
   events.

ggtags-find-definition

   Find definition tags. With ``C-u`` ask for the tag name with
   completion.

ggtags-find-reference

   Find reference tags. With ``C-u`` ask for the tag name with completion.

ggtags-find-other-symbol

   Find tags that have no definitions. With ``C-u`` ask for the tag
   name with completion.

ggtags-find-tag-regexp

   Find definition tags matching a regexp. By default it lists all
   matching tags in the project. With ``C-u`` restrict the lists to a
   directory of choice.

ggtags-idutils-query

   Use idutils to find matches.

ggtags-grep

   Grep for lines matching a regexp. This is usually the slowest.

ggtags-find-file

   Find a file from all the files indexed by ``gtags``.

ggtags-query-replace

   Do a query replace in all files found in a search.

Handling multiple matches
+++++++++++++++++++++++++

When a search finds multiple matches, a buffer named
``*ggtags-global*`` is popped up and ``ggtags-navigation-mode`` is
turned on to facilitate locating the right match.
``ggtags-navigation-mode`` makes a few commands in the
``*ggtags-global*`` buffer globally accessible:

``M-n``

   Move to the next match.

``M-p``

   Move to the previous match.

``M-}``

   Move to next file.

``M-{``

   Move to previous file.

``M-=``

   Move to the file where navigation session starts.

``M-<``

   Move to the first match.

``M->``

   Move to the last match.

``C-M-s`` or ``M-s s``

   Use ``isearch`` to find the match.

``RET``

   Found the right match so exit navigation mode. Resumable by ``M-,``
   (``tags-loop-continue``).

``M-*``

   Abort and go back to the location where the search was started.

Miscellaneous commands
++++++++++++++++++++++

Commands are available from the ``Ggtags`` menu in ``ggtags-mode``.

ggtags-prev-mark

   Move to the previously (older) visited location. Unlike ``M-*``
   this doesn‘t delete the location from the tag ring.

ggtags-next-mark

   Move to the next (newer) visited location.

ggtags-view-tag-history

   Pop to a buffer listing all visited locations from newest to
   oldest. The buffer is a next error buffer and works with standard
   commands ``next-error`` and ``previous-error``. In addition ``TAB``
   and ``S-TAB`` move to next/prev entry, and ``RET`` visits the
   location. ``M-n`` and ``M-p`` move to and display the next/previous
   entry.

ggtags-view-search-history

   View or re-run past searches as kept in
   ``ggtags-global-search-history``.

ggtags-kill-file-buffers

   Kill all file-visiting buffers of current project.

ggtags-toggle-project-read-only

   Toggle opening files in ``read-only`` mode. Handy if the main
   purpose of source navigation is to read code.

ggtags-visit-project-root

   Open the project root directory in ``dired``.

ggtags-delete-tags

   Delete the GTAGS, GRTAGS, GPATH and ID files of current project.

ggtags-explain-tags

  Explain how each file is indexed in current project.

ggtags-browse-file-as-hypertext

   Use ``htags`` to generate HTML of the source tree. This allows
   browsing the project in a browser with cross-references.

使用举例

1,从调用一个函数的地方跳转到函数的定义的地方

M-x ggtags-find-definition

2,从调用一个函数的地方跳转到函数的定义的地方后,返回到调用函数的地方

M-x ggtags-prev-mark

3,或是反过来从函数定义的地方列出所有调用这个函数的地方

M-x ggtags-find-reference

4,查看导航过的历史记录

M-x ggtags-view-tag-history

原文地址:https://www.cnblogs.com/xiaoshiwang/p/9204418.html

时间: 2024-07-31 14:34:10

用emacs 阅读 c/c++ 代码的相关文章

Emacs阅读chm文档

.title { text-align: center; margin-bottom: .2em } .subtitle { text-align: center; font-size: medium; font-weight: bold; margin-top: 0 } .todo { font-family: monospace; color: red } .done { font-family: monospace; color: green } .priority { font-fami

如何通过阅读别人的代码提高自己的编程能力

代码阅读的必要性 阅读别人的代码作为研发人员是一件经常要做的事情.一个是学习新的编程语言的时候通过阅读别人的代码是个最佳的学习方法,另外是积累编程经验.如果你有机 会阅读一些操作系统的代码会帮助你理解一些基本的原理.更有就是在你作为一个质量确保人员或一个小领导的时候如果你要做白盒测试的时候没有阅读代码的能力 是不能完成相应的任务.最后一个就是如果你中途接手一个项目的时候或给一个项目做售后服务的时候是要有阅读代码的能力的. 收集所有可能收集的材料阅 读代码要做的第一件事情是收集所有和项目相关的资料

如何阅读一份代码?

https://zhuanlan.zhihu.com/p/26222486 ****************************** 上文谈到了像读书一样阅读源码的重要性,今天谈谈如何阅读一份代码.我所谓的一份代码,其范围可能从几千行到数万行,有时甚至可多达数十万行.这些代码作为一个有机体,共同完成某些重要的功能.比如说几个著名的 full fledged web framework,祖师爷 rails,师叔 django 和小师妹 phoenix: 三者对比很有意思 - rails / d

Kaldi阅读并更改代码

Common utilities base/kaldi-common.h 几乎所有Kaldi程序都会include该头文件. 该头文件include了一些其他位于base/目录的头文件,主要提供: 错误-日志 宏 类型定义(typedefs) 数学实用程序函数(如随机数生成器) 其他#defines util/common-utils.h 命令行参数解析 I/O函数(处理带管道的文件名) ark列表处理 列表类型 字符串转换 gmm/model-common.h GMM/SGMM的枚举,如:模型

阅读面向对象的代码:我们要有两个关注点:

阅读面向对象的代码:我们要有两个关注点:对象如何被创建,创建流程是什么样的:对象是如何被组织的,也就是被谁引用了:对象的方法是如何被调用的,也就是方法在什么地方,什么时机被触发执行的:如果对上述三点能够很好的把我,那么阅读代码也是一种剥丝抽茧的过程另外,在分析调用关系时,一定要有一个主链条,不能什么都要了解的非常透彻,要对你当下分析的主流程进行重点分析(其他涉及到的点点滴滴可以暂时搁置,后面再分析): 原文地址:https://www.cnblogs.com/big1987/p/10898963

如何阅读别人的代码

############################################## """ 阅读什么样的代码? 很难明确的说应该阅读什么样的代码,只能说有一些基本的原则: 1,阅读那些你用得着的代码 这一步最好从阅读那些你已经在使用的插件或者库文件开始. 这些都是你可以学习的非常好的例子.你已熟悉了它们的公共API,了解它们内部工作原理的障碍是较低的.此外,作为代码的用户,你有机会添加文档.实现新的功能,或者通常以某种方式给项目做贡献. 2,阅读那些你印象深刻的代码

拿什么心情来阅读我的代码(程序员的必备心理技能)

原文首发于我的微信公众号:GeekArtT . 阅读源代码的开始阶段,最好从感兴趣.自己有直观感受且有相对丰富准确的文档的项目开始.如同最开始阅读数学证明,最好从浅显易懂的教材开始,之后再开始最前沿的paper阅读. 阅读源代码需要同阅读证明一样的耐心,甚至更多.数学的定义或许就在前一页,可是源代码的某个新的类却需要你不断地Google,进而找到适合自己的关于原始概念的定义论述. 阅读源代码一定要放空自己的心,要做好精心死磕的准备.至少要在心理上给自己留出足够多的空间与时间.大不了拿出大段时间去

利用Eclipse CDT 阅读C/C++代码

本文转自: https://xbgd.iteye.com/blog/1259544 常见阅读代码的工具有 , visual c++, visual studio + va(visual assistant), 以及 source insight, 这几个功能强大的收费软件 , 目前用得非常多 , 尤其是 Source Insight( 以下简称 SI), 是一个不错的跨平台的软件 . 今天推荐的 Eclipse C++ 版本 , 同样也是一个优秀的开源阅读代码的工具 , 其优越性有甚于 SI, 

windows和ubuntu虚拟机设置共享文件夹,在windows中用source insight阅读和编写代码,在linux中用交叉编译工具编译。

1:公司有个HISI3536的项目,需要用到交叉编译工具,因此需要安装ubuntu虚拟机,然后:在ubuntu中用交叉编译工具进行编译,刚开始自己是把整个的工程文件和代码都放到了ubuntu中,在ubuntu中进行代码的阅读和编写,然后在ubuntu中进行编译,然后还要把编译完成的可执行程序上传到windows中再下载到设备中.麻烦而且容易出错. 2:项目完成之后觉得这样做太麻烦了,想找一下有没有简单点的方法,突然想起来之前用过设置文件夹共享的方式,于是设置了文件夹共享,然后代码阅读和编写都在w