By Dave Hillier

2008-10-25 08:14:04 8 Comments

I've recently been trying to create units tests for some legacy code.

I've been taking the approach of using the linker to show me which functions cause link errors, greping the source to find the definition and creating a stub from that.

Is there an easier way? Is there some kind of C++ parser that can give me class definitions, in some easy to use form, from which I can generate stubs?


@linuxbuild 2012-09-22 19:20:54

The abi-compliance-checker tool can be used as a parser of C/C++ header files:

abi-compliance-checker -lib NAME -dump VER.xml -headers-only -xml -stdout > api.xml

VER.xml input file is the following:



The output api.xml file contains function signatures and other information from header files in the structured form:


See also information about api-sanity-checker tool, that can generate basic unit test cases for every function in the API through the analysis of declarations in header files.

@tully 2008-12-13 01:54:52

Eclipse CDT project provide an advanced C++ parser. The interface is quite easy. The following code snippet can give enough hint.

ITranslationUnit tu = CoreModelUtil.findTranslationUnit(file);
ICElement[] elements = tu.getChildren();

IStructure structure = (IStructure) element;
IMethodDeclaration[] methods = structure.getMethods();
IField[] field = structure.getFields();

@philant 2008-12-08 12:28:59

Did you look at Mockcpp, AMOP and mockpp ? You could see how they parses C++ - if none of them fit your needs.

@zvrba 2008-10-25 16:45:56

If you're on aplatform that uses DWARF debugging format (mostly UNIX), you can use libdwarf to parse debugging information and extract information about everything (function prototypes, class definitions, etc). Much more structured and easier to parse than C++.

@Dave Hillier 2008-10-25 11:45:46 looks promising but is incomplete. uses GCCXML to generate wrappers for C++ code to interface python. This proves that GCCXML has been used for a similar concept.

@anatoly techtonik 2015-01-27 13:47:42

The answer might be outdated since 2008. See‌​an for more modern overview of this capability of clang.

@CesarB 2008-10-25 12:09:25

doxygen can usually parse enough of C++ to create documentation for the code. It also has a XML output option.

@Ferruccio 2008-10-25 11:25:39

If you're on the Windows platform, you might want to have a look at the Microsoft Phoenix project. It's a new compiler framework that lets you hook into any stage of the compilation process.

@Dwight Holman 2008-10-25 09:31:15

Gcc XML is used in some projects, such as automatic FFI for Common Lisp. It ties into the G++ compiler to generate XML representing the source. From there, any XML processing tool could help you reach your goal.

@PaweĊ‚ Hajdan 2008-10-25 09:15:54

You may want to investigate But C++ parsing is hard.

On the other hand, maybe ctags or something similar can extract class definitions...

You may also try to write your own simple (?) parser to generate class stubs from header files...

I tried to give you some pointers. As you see, the problem is not easy. But hopefully you can automate at least some part of it.

@Dave Hillier 2008-10-25 11:07:50

Good answer but seems fairly old. Anyone know of more up to date sources?

Related Questions

Sponsored Content

21 Answered Questions

[SOLVED] What is the "-->" operator in C++?

22 Answered Questions

[SOLVED] How do I parse a string to a float or int?

35 Answered Questions

[SOLVED] What's the difference between a mock & stub?

  • 2010-08-11 14:19:57
  • never_had_a_name
  • 271335 View
  • 807 Score
  • 35 Answer
  • Tags:   testing mocking stub

24 Answered Questions

[SOLVED] Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

48 Answered Questions

30 Answered Questions

[SOLVED] How do you parse and process HTML/XML in PHP?

34 Answered Questions

31 Answered Questions

[SOLVED] Unit Testing C Code

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2152054 View
  • 4248 Score
  • 1 Answer
  • Tags:   c++ c++-faq

16 Answered Questions

[SOLVED] Parse JSON in JavaScript?

Sponsored Content