Generate Ctags files for C + + Source files and all of the their Included Header files

Source: Internet
Author: User

Original address: 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 was for those people who use exuberant Ctags. If you is using other versions of Ctags, the This post is not being useful.

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

ctags -R .

For some users (need) more info of the symbols, they could use this command instead:

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

No matter which one use, the generated tags file is contains the symbols in the files in your project source tree, b UT not no external file, such as standard header files (e.g. stdio.h, stdlib.h), etc. thus editors or IDEs this use tags files, such as Vim, is not able to locate symbols in external header files. There is a solution:generate a tags file for any external headers files first, and let the editor or IDE read both the GE nerated tags file and the tags file for the project source tree. For example, the following command would generate a tags file for all your system headers 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 edit Or or IDE a long time to search this tags file for symbols. To solve the problem, I came up with the another idea.

Why must we generate a tags file containing all the symbols in the system header? If we only generate the tags file is related to our projects, would it is faster? That's the point of this idea. We could first search for the headers files that is included in our projects, and then we use Ctags to generate a tags fil E for these files and we source files, in this is a much smaller tags file that containing all the symbols that maybe U Seful for the project is generated.

To does this, I wrote a shell script:

Ctags_with_dep.shdownload the script

1234567        
 #!/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 ' Span class= "P" >|  Sed-e  '/^$/d '-e  '/\.o:[\t]*$/d ' |  Ctags-l---c++-kinds=+p--fields=+iaS--extra =+q               

This script was also available on GitHub gist. If you have want to use it, download the script with 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 "s happening here. This script would first Use  gcc-m  to Output the list of the headers files that is included in our C or C + + source files. However, the output could not being directly used by ctags, thus this script Uses se D commands to filter the output. Finally, this script uses A pipe  to put the file list to The stdin  of the Ctags program–ctags would read the file list from stdin if  -L -  is passed to it in the command line.

What if you had other directories besides the Standard  /usr/include  that containing the header files you need? You could does a little modification on this script. For example. Some header files In  ~/include , then you Could pass  -I ~/include  to the GCC command. Just like below:

123 
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 had any suggestion on the this idea, please let me know.

Generate Ctags files for C + + Source files and all of the their Included Header files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.