Generate Ctags Files for C/C++ Source Files and All of Their Included Header Files
2014-09-20 10:01
471 查看
原文地址: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:
For some users that need more info of the symbols, they may use this command instead:
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:
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
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:
Read on if you want to know what’s happening here. This script will first use
What if you have other directories besides the standard
If you have any suggestion on this idea, please let me know.
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 |
./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 -Mto 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/includethat 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 ~/includeto 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 |
相关文章推荐
- Source Files and Programs(Chapter 9 of The C++ Programming Language)
- A solution for conflict of structure loc_t in header files of vac compiler and informix esql on AIX
- Generate C and C++ Header File
- generate a simple class for all fields of a table .
- A Free , Fast and Small Automatic Formatter for C , C++ , C# , Java Source Codes
- Where does Visual Studio look for C++ Header files?
- 关于 VC6.0 安装时候的问题C/C++ Header File 和 C++ Source Files
- Reset textruemap _path of ZIP and copy allfiles to samepackage
- What is required for a successful backup of all files during hoi backup?
- python-2.7.6 编译 node_gyp_header_files_for_hex_1.1.6_1621_windows32 库进行生成C++项目为什么报错,是缺少什么东西么。
- [MSDN]TN035: Using Multiple Resource Files and Header Files with Visual C++
- [论文笔记] Leveraging the crowd as a source of innovation Does crowdsourcing represent a new model for product and service innovation? (SIGMIS-CPR, 2012)
- C++:unexpected end of file while looking for precompiled header directive
- Kettle解析JSON错误,We MUST have the same number of values for all paths,We can not find and data with path [$.
- 69.You need to generate a list of all customer last names with their credit limits from the CUSTOMER
- TN035: Using Multiple Resource Files and Header Files with Visual C++
- Explanation of Cross Domain and Client Access Policy files for Silverlight
- (Sharepoint 2010) Change the background and font color of text for a web part header
- Display a Directory Tree of all Subdirectories and Files
- Source code for SIFT, ORB, FAST and FFME for OpenCV C++ for egomotion estimation