Generate Ctags Files for C/C++ Source Files and All of Their Included Header Files

原文地址:http://www.topbug.net/blog/2012/03/17/generate-ctags-files-for-c-slash-c-plus-plus-source-files-and-all-of-their-included-header-files/

This post is for those people who use Exuberant Ctags. If you are using other versions of ctags, this post may not be useful.

When using ctags to generate the tags file for C/C++ projects, usually we use the following command:

ctags -R .

For some users that need more info of the symbols, they may use this command instead:

ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .

No matter which one you use, the generated tags file only contains the symbols in the files in your project source tree, but not any external file, such as standard header files (e.g. stdio.h, stdlib.h), etc. thus editors or IDEs that use tags files, such as Vim, are not able to locate symbols in external header files. There was a solution: generate a tags file for any external header files first, and let the editor or IDE read both the generated tags file and the tags file for the project source tree. For example, the following command will generate a tags file for all your system header files on UNIX/Linux:

ctags -R --c++-kinds=+p --fields=+iaS --extra=+q /usr/include

This command usually takes a very long time to finish, and finally it gives a quite large tags file, which causes the editor or IDE a long time to search this tags file for symbols. To solve this problem, I came up with another idea.

Why must we generate a tags file containing all the symbols in the system header? If we only generate the tags file only for the header files that are related to our projects, would it be faster? That’s the point of this idea. We could first search for the header files that are included in our projects, and then we use ctags to generate a tags file for these files and our source files, in this way, a much smaller tags file that containing all the symbols that maybe useful for the project is generated.

To do this, I wrote a shell script:

ctags_with_dep.shDownload the script

1
2
3
4
5
6
7
#!/bin/sh

# ./ctags_with_dep.sh file1.c file2.c ... to generate a tags file for these files.

gcc -M $* | sed -e ‘s/[\\ ]/\n/g‘ |         sed -e ‘/^$/d‘ -e ‘/\.o:[ \t]*$/d‘ |         ctags -L - --c++-kinds=+p --fields=+iaS --extra=+q

This script is also available on github gist. If you only want to use it, download the script and use the following command to generate the tags file:

./ctags_with_dep.sh file1.c file2.c file3.cpp ...

Read on if you want to know what’s happening here. This script will first use gcc -M to output the list of header files that are included in our C or C++ source files. However, the output could not be directly used by ctags, thus this script uses sed commands to filter the output. Finally, this script uses a pipe to put the file list to the stdin of the ctags program – ctags will read the file list from stdin if -L - is passed to it on the command line.

What if you have other directories besides the standard /usr/include that containing the header files you need? You could do a little modification on this script. For example, you have some header files in ~/include, then you could pass -I ~/include to the gcc command. Just like below:

1
2
3
gcc -M -I ~/include $* | sed -e ‘s/[\\ ]/\n/g‘ |         sed -e ‘/^$/d‘ -e ‘/\.o:[ \t]*$/d‘ |         ctags -L - --c++-kinds=+p --fields=+iaS --extra=+q

If you have any suggestion on this idea, please let me know.

时间: 2024-12-12 03:07:50

Generate Ctags Files for C/C++ Source Files and All of Their Included Header Files的相关文章

Enter the path to the kernel header files for the 3.18.0-kali1-686-pae kerne vmware tool

安装VMWare Tools出现提示:Enter the path to the kernel header files for the 3.18.0-kali1-686-pae kerne? 2015年02月15日 渗透测试 评论 1 条 阅读 4,421 次 由于缺少头文件导致的,只要安装linux header即可: 1.先更新软件源: 先备份软件源sources.list文件,然后修改sources.list文件,在终端输入: leafpad /etc/apt/sources.list

C header files matching your running kernel were not found. Refer to your distribution's documentation for installation instructions

[email protected]:~# apt-get install linux-headers-$(uname -r) [email protected]:~# apt-get install build-essential linux-kernel-headers C header files matching your running kernel were not found. Refer to your distribution's documentation for instal

关于VMware的“Enter the path to the kernel header files...?”报错的解决方法

这是由于在安装Vmware tools时找不到kernel header files,因此只要安装相关依赖包即可 sudo apt-get update sudo apt-get install build-essential sudo apt-get install libx11-6 libx11-dev libxtst6 xinetd sudo apt-get install linux-headers-`uname -r` build-essential

解决Cannot find MySQL header files under /usr/include/mysql的错误

按照下面的步骤能成功,亲测.转帖,做笔记 编译php-5.5-6的mysql支持,出现Cannot find MySQL header files under /usr/include/mysql. Note that the MySQL client library is not bundled anymore!错误!解决方法如下: [[email protected] php-5.5.6]# ./configure --prefix=/usr/local/php --with-config-

编译安装php时提示Cannot find MySQL header files的解决方法

php的配置文件中有一行--with-mysql=/usr/local/mysql ,安装的时候提示:configure: error: Cannot find MySQL header files under yes.Note that the MySQL client library is not bundled anymore. 这是由于安装mysql时没有安装mysql头文件,或者是路径指定不正确,php找不到mysql的头文件引起的错误提示. 解决方法.1. 查看你的系统有没有安装my

linux下安装php报错configure: error: Cannot find MySQL header files under /usr/include/mysql.

linux下安装php报错configure: error: Cannot find MySQL header files under /usr/include/mysql. 2013-03-04 15:34wdjhz | 分类:服务器软件 | 浏览5318次 configure: error: Cannot find MySQL header files under /usr/include/mysql.Note that the MySQL client library is not bun

在fedora14下安装vmware 7.1.4 build-385536 出现C header files matching your running kernel were not found. 错误

1.到官网找到最新版本的的vmware 这里我用的是当时最新的 7.1.4 build-385536 2.在root下通过命令chmod u+x 给安装包执行权限,再安装,应该没有问题 3.刚安装完了以后(最好重启一下),启动,出现错误(在redhat5.4版本没有此问题): C header files matching your running kernel were not found. Refer to your distribution's documentation for inst

如何处理错误消息Please install the Linux kernel header files

Please install the Linux kernel "header" files matching the current kernel 当我启动minilkube时遇到如下错误消息: Minikube setup with driver virtualbox Starting local Kubernetes v1.10.0 cluster... Starting VM... E1010 03:57:24.565157 9896 start.go:174] Error s

解决mysql跟php不在同一台机器上,编译安装php服务报错问题:configure: error: Cannot find MySQL header files under /application/mysql.

在编译安装php服务时报错: configure: error: Cannot find MySQL header files under /application/mysql. Note that the MySQL client library is not bundled anymore! 前边搭建lnmp环境时,是把mysql和php安装在了同一台机器上,编译php的时候,需要通过参数 --with-mysql来指定mysql的安装路径,但在生产环境中,通常php和mysql是不在同一台