Question

Does anyone have a recommended way to find definitions in header files when using gcc? When using MSVC, I can just right-click and select "Go to definition" which is really nice.

I have used netbeans + gcc, and it does have code assistance including hyperlinking to the definition, so that is one option. However, I would like to know if there are any other IDEs with that functionality and also the best way when not using an IDE.

Was it helpful?

Solution

You can run Doxygen to generate an HTML-based source browser. This does not require that the code be annotated with Doxygen-style documentation. It works for multiple language, including C++, Java, and Markdown (.md files go to "Related Pages").

Here's a way of configuring and launching Doxygen from the command-line (tested on Linux)...

## basic
echo -e "SOURCE_BROWSER=YES\n EXTRACT_ALL=YES\n RECURSIVE=YES\n" |doxygen -
xdg-open html/index.html

or

## include diagrams and non-public content -- and launch browser immediately
echo -e "HAVE_DOT=YES\n CALL_GRAPH=YES\n CALLER_GRAPH=YES\n SOURCE_BROWSER=YES\n EXTRACT_ALL=YES\n EXTRACT_PRIVATE=YES\n EXTRACT_STATIC=YES\n RECURSIVE=YES\n SEPARATE_MEMBER_PAGES=YES\n GENERATE_LATEX=NO\n EXCLUDE_SYMLINKS=YES" |doxygen - && chromium-browser --new-window html/index.html

There is a companion program called doxywizard which lets you browse through the many configuration options available for customizing the generated HTML. Alternatively, you can run doxygen -g to create an initial configuration file (which includes detailed hints).

I recommend the following non-default options to generate a source browser:

SOURCE_BROWSER=YES ## display source code
EXTRACT_ALL=YES ## display all items (not just "documented" ones)
RECURSIVE=YES ## include source found in subdirectories
SEPARATE_MEMBER_PAGES=YES ## optional -- each class member has its own page

Once you have the main web page up, you can browse to the source of interest (or use the search feature). The source is cross-referenced with clickable links to relevant declarations.


I've run into this problem a few times: doxygen can create really long filenames and Linux encrypted home directory has a limit of 143 characters. To work around this, you may have to write the html folder outside of your home.

A quick and dirty solution is to link html to a folder in /tmp or /dev/shm -- maybe also chmoding the permissions for better data security.

OTHER TIPS

Try CScope. Together with the Emacs module xscope you can tell Emacs to M-x cscope-find-global-definition, which will jump to the file in which a symbol is defined. Very handy.

Of course an IDE such as Eclipse or KDevelop or Code::Blocks should have such a feature, too.

Eclipse CDT is platform-independent and has such feature (plus more). It indexes all includes you have in your project (library + your own), so you can find definition of any declaration (or, should I say, any declaration). You can think of Eclipse as of fancy C++ oriented text editor with additional capabilities to launch make (nmake.exe or whatever) with your makefile. I use Eclipse + MS cl.exe compilers from WinSDK - they work like charms together. I run nmake.exe from Eclipse, and Eclipse redirects nmake.exe console nicely into special IDE window. It also parses compiler's output and automatically highlights offending lines. The only thing that one needs to do is to set up environment variables for nmake.exe from within Eclipse.

cscope is one of my favorite tools for understanding C code but it may not translate well to the Windows world.

exuberant ctags is an excellent tool for finding definitions in dozens of languages. Most reasonable programming editors provide some interface for calling into ctags for definition information. (vim, for example, allows you to use ^] to find a definition of a symbol and ^T to pop back up the definition stack.) You build a database using ctags -R . and look up definitions using your editor. (For example, vim -t foo will look up the foo tag in the ctags database and jump to that file and line.)

The GNU idutils package provides some nice utilities similar to ctags for finding uses of symbols in a variety of languages. Integration isn't as good in most editors as ctags or cscope but it might be easier for you to write tools. You build the database with mkid . and look up symbols with gid foo or lid foo or similar lookups.

I find the three tools provide different enough functionality that I use all three commonly. ctags makes hunting through source in vim extremely quick, cscope can help find callers and callees, and gid (especially gid -s ...) is good for just getting some hints on complex situations that the other tools don't just have solved. (Neither ctags nor cscope understands token pasting well, but gid -s lets me approximate a solution myself.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top