85

Do you have any sugestions of tools to ease the task of understanding C/C++ code? We just inherited a large piece of software written by others and we need to quickly get up to speed on it. Any advice on tools that might simplify this task?

Dprado
  • 1,610
  • 1
  • 13
  • 13
  • 20
    @casperOne: why is this "not constructive"?! It's useful to anyone ...and constructive! – dagnelies Aug 31 '12 at 14:30
  • @arnaud Shopping list questions are not constructive. http://blog.stackoverflow.com/2010/11/qa-is-hard-lets-go-shopping/ – casperOne Aug 31 '12 at 14:31
  • 9
    @casperOne: the article you mention describes questions about recommendations for buying hardware, and so on. Definitely questions to reject. In this case, it is a question about tools and techniques to solve a particular problem, namely helping understanding large code. I personally find the bridge between the two quite big. – dagnelies Aug 31 '12 at 14:39
  • @arnaud They're the same, just because you aren't spending money on it or buying it (or maybe you are) doesn't mean you're not trying to solicit a list of things. Additionally, it doesn't matter if it's hardware or libraries or tools, there's no way to objectively indicate what is better than the other. See this Meta post for more information on why list questions are not constructive: http://meta.stackexchange.com/q/138411/140951 – casperOne Aug 31 '12 at 14:41
  • 1
    http://www.grok2.com/code_comprehension.html – Ciro Santilli OurBigBook.com Jul 04 '15 at 05:56
  • 1
    Because this is Stack Overflow. Here, every question is either not constructive, stupid, or closed as duplicate. And everyone can edit your posts to completely change its meaning so that they could answer the question you never asked and get some points easier. – SasQ Apr 30 '19 at 18:16
  • This is the best place to visualise C code for learning: http://www.pythontutor.com/c.html – pavi2410 Sep 09 '20 at 19:01

16 Answers16

26

SourceInsight and Understand for C++ are the best tools you can get for c/c++ code analysis including flow charts.

aku
  • 122,288
  • 32
  • 173
  • 203
10

Profiling software gives you an idea of which functions have been called.

If you can use Linux, try KCachegrind

ISanych
  • 21,590
  • 4
  • 32
  • 52
Thorsten79
  • 10,038
  • 6
  • 38
  • 54
9

Doxygen is very good at generating diagrams from code without applying markup, if you turn on the EXTRACT_ALL option. You need GraphViz installed to get diagrams generated with the HAVE_DOT setting. I find having it installed and leaving the DOT_PATH blank works fine on Windows but on OS/X keep having to point directly to the dot tool location.

There's an excellent Code Spelunking article in ACM Queue which talks more about using doxygen and DTrace.

albert
  • 8,285
  • 3
  • 19
  • 32
Andy Dent
  • 17,578
  • 6
  • 88
  • 115
8

Personally, I use the debugger. Run through the code and see what its doing, and where its going is the only way.

However, you can run it through some documentation-generators which (sometimes) help. A good example is doxygen.

gbjbaanb
  • 51,617
  • 12
  • 104
  • 148
  • +1 Debugger is the only way to understand the code. – Tegiri Nenashi Dec 13 '10 at 20:48
  • 8
    I find trying to understand a *large* code base that you have no experience with pretty ridiculous. Try doing that on a project like Clang or Linux. You'll go insane. Good for code I'm familiar with, or small/medium size projects IMHO. – Charles Jul 31 '14 at 21:45
  • 1
    I came here from google because I've been using debugger to find where to change code in huge project with no success for four days now. – Tomáš Zato Sep 17 '15 at 07:47
5

KScope, built upon the cscope utility, if you're on linux (KDE). The best thing I ever used (and use all the time) to deleve into some huge piece of unfamiliar code which I have to modify somehow or which API I'm to employ for my needs. Among its features are the cross-reference database, which can be searched in a plenty of ways: you can find all references of a symbol, its global definition, find callers/callees of a function and much more. It even has a built-in IDE and an ability to show a call-graph.

4

Doxygen will give you class relationship diagrams if you use graphviz, even if you haven't specifically prepared for it.

Douglas Mayle
  • 21,063
  • 9
  • 42
  • 57
3

There are some tools like Egypt http://www.gson.org/egypt/egypt.html that work, but only if you match the expected GCC version used to compile the code and the exact version of the callgraph generator. The same can be said about codeviz http://www.csn.ul.ie/~mel/projects/codeviz/

Other option is valgrind used in cachegrind mode (it generates a type of callgraph that you can follow from kcachegrind program.

1

I personnaly use Visual Studio debugger tools.

It comes with the "Caller Graph" feature which will allow you to visualize stuff in little boxes. Also the Call Stack and the usual watch features are usually all I need

Eric
  • 19,525
  • 19
  • 84
  • 147
  • Where is Caller Graph in Visual Studio debugger tools? I can find only Caller Hierarchy in Visual Studio non-debugger tools. – Hyunjik Bae Oct 15 '17 at 05:14
1

There's also AspectBrowser which doesn't work very good with Eclipse 3.4.0

user23787
  • 11
  • 1
1

try AQtime, It's a profiling tool that displays all the functions that got called (and the time it took), you can set the start and end points for the analysis. They have a 30 day trial.

LLucasAlday
  • 2,349
  • 11
  • 34
  • 41
  • AQtime's call graphs are sometimes very illuminating even if you think you know the code. – bk1e Oct 01 '08 at 07:15
1

I used Borland Together a while back and it did a decent job of generating models from code. I believe it will even generate sequence diagrams from code. Keep in mind if your code is a mess your model will be too. And as I recall it isn't cheap but sometimes you can catch a special.

Craig Tyler
  • 470
  • 6
  • 9
1

Rational Quantify also presents a nice call graph.

QBziZ
  • 3,170
  • 23
  • 24
0

doxygen is a free doc-generating tool (similar to Javadoc) that will also produce relationship graphs as well.

albert
  • 8,285
  • 3
  • 19
  • 32
luke
  • 36,103
  • 8
  • 58
  • 81
0

Doxygen, the good thing about it is it will let you know hoe ugly/good is your code in terms of cyclic dependency of classes. So you will be forced to re-factor your code, though you may not like it :-)

rptony
  • 1,024
  • 1
  • 12
  • 22
0

Slickedit is great for navigating large blocks of code you don't know. The tags feature allows you to deal with the code on a functional basis, without having to deal with which file is it in. (EMACS actually has tags and is every bit as good as Slickedit, but with a slightly steeper learning curve)

When you get to a method or class or variable you don't understand, you just push-tag to go to that code, look it over, then pop-tag back. (those are bound to keystrokes, so it is very quick)

You can also use find-references to see where that function/variable is used.

Saves tons of time over having to go and figure out which file something is in.

boatcoder
  • 17,525
  • 18
  • 114
  • 178
0

i tried a tool named Visustin which is not very great graphically but does what it says: Flowchart from Code. http://www.aivosto.com/visustin.html

rjdthegreat
  • 204
  • 3
  • 10