Question

Is there a way to generate a c++ header file for an .obj file? Or perhaps is there an utility that can view .obj files. I've already found objconv that converts between formats, but I can't find any .h generator/viewer.

Was it helpful?

Solution

Given the C++ tag, the situation isn't quite as hopeless as some of the other answers imply.

In particular, at least with most C++ compilers, the name of a function will be mangled (Microsoft calls it "decorated") to indicate the parameters taken by that function. The mangling scheme varies from one compiler to another, but essentially any of them encodes enough information for you to re-create a declaration for the function -- the return type, the name of the function itself, the class name if it's a member function, and the exact number and type of parameters the function expects.

Though it wouldn't have to be done by mangling the name, a C++ system has no real choice but to include parameter information in the object file. When you overload functions, the linker needs some way to sort out which overload to link up with a particular call.

I should add that sorting this all out may be quite a bit of work though -- just for example, if the code includes templates, the symbol name in the object file will expand out all the template parameters (including default parameters). Just for example, consider a trivial bit of code like this:

#include <vector>
#include <string>

std::vector<std::string> x;

When I compile this with VC++, and then do dumpbin /symbols foo.obj, the output is about 75 kilobytes. Even though my source code only appears to define one symbol, the object file contains definitions for around 180 symbols, many of them almost completely unrelated to anything I wrote at all.

It's also worth mentioning that if the object file was compiled with debugging information, that will (normally) produce substantially more type information (and such) in the object file as well.

OTHER TIPS

Your question is tagged with visual-c++ so I assume you're on Windows.

Generating the original header files out of object files is not possible, since much (arguably most) of the information in header files is dropped while compiling the source code into object files.

However, you can use the dumpbin utility to inspect object files at a very low level. The only information present in an object file which might be interesting to you is the list of symbol names which are defined in the given object file. You can't get any type information from the object file though. So you cannot tell what type of value a given function takes or expects.

In some situation (when all arguments are passed to the function via the stack) you could possibly tell the number of arguments which a function expects (by disassembling the start of the function code and counting how many values are popped from the stack). That won't work in the vast majority of cases though.

There cannot be a header generator, as the .obj file does not contain all the information that would be necessary in a header file. As for dumping the .obj, the tools to do that will be compiler dependent. The GCC-related tool to do this is objdump.

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