8

I am moving development to Linux but I couldn't find how I can get an output similar to /d1reportSingleClassLayout from MSVC++ under g++ or clang++. If these compiler do not have such a feature, is there an external tool that provides similar visualization?

bolov
  • 72,283
  • 15
  • 145
  • 224
  • 1
    You'd be better off asking this on stack overflow, since this is really more a C++ question than anything else; particular compiler features like this are not very relevant to the OS the compiler is running on. – CodeClown42 Apr 11 '13 at 14:16
  • Yep, caught the edit inside 5 min ;) Thx. I've flagged it too. – CodeClown42 Apr 11 '13 at 14:22
  • Thanks for your quick responses! I thought since I want a linux solution it would be appropriate here. Sorry for the noise. –  Apr 11 '13 at 14:25

3 Answers3

8

You need to compile the file with debugging information (-g option) then use pahole to display the struct layout. pahole is usually available from the dwarves package (source; on GitHub; in Ubuntu).

$ g++ -ggdb -c -o myfile.o myfile.cpp
$ pahole -C MyClass myfile.o
class MyClass {
public:

    int ()(void) * *           _vptr.MyClass;        /*     0     4 */
    int                        i;                    /*     4     4 */
    const char  *              c;                    /*     8     4 */
    void MyClass(class MyClass *, const class MyClass  &);

    void MyClass(class MyClass *);

    virtual void ~MyClass(class MyClass *, int);


    /* size: 12, cachelines: 1, members: 3 */
    /* last cacheline: 12 bytes */
};

The -C option lets you select which class/struct to examine.

If you don't have access to pahole you can get the same information in a much less readable form from readelf -wi myfile.o or eu-readelf -winfo myfile.o. The paper https://www.kernel.org/doc/ols/2007/ols2007v2-pages-35-44.pdf describes pahole alongside other dwarves tools.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
1

You can also do it with the Clang frontend.

Jean-Bernard Jansen
  • 7,544
  • 2
  • 20
  • 17
0

How about -fdump-class-hierarchy supported by g++?

Community
  • 1
  • 1
nodakai
  • 7,773
  • 3
  • 30
  • 60