What is a good way to find definitions in header files when using gcc?
Asked Answered
I

4

3

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.

Ironwork answered 16/1, 2012 at 0:46 Comment(5)
All IDEs with proper C++ support should offer this ability.Formulate
@OliCharlesworth: Yes, but what is a good way when not using an IDE other than say windows search? (Im on windows).Ironwork
What tool are you using to write your code? The answer typically depends upon which editor you're using.Yardarm
@sarnold: Currently notepad++ (I use an IDE also), but I still enjoy using a text editor + make sometimes for testing, etc.Ironwork
You can use Visual Studio to edit, then compile with gcc. I think you'll find this surprisingly productive.Trove
P
3

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.

Papillon answered 16/1, 2012 at 4:17 Comment(2)
Thanks for the info, one thing I found out, is libstdc++ already generates there documentation using doxygen!.Ironwork
Related: how to get doxygen to produce call & caller graphs for c functionsPapillon
A
2

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.

Anzac answered 16/1, 2012 at 0:52 Comment(2)
Thanks, CScope seems to be popular on Unix, when I looked at the page and saw "may show all kinds of strange bugs on 2000 and XP", I thought it might be not the best solution. Also, the more I think about it, the best solution is probably "use an IDE".Ironwork
clang.. ctags, gcc-xml.. as well.Lazaretto
T
1

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.

Trowel answered 16/1, 2012 at 3:22 Comment(0)
Y
0

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.)

Yardarm answered 16/1, 2012 at 3:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.