我读过一篇关于 dumping a C++ object's memory layout with Clang 的文章现在我正在尝试使用此功能。我创建了两个类:
// simple.cpp
struct Base
{
int value;
};
struct Derived : Base
{
int count;
};
int main(int argc, char* argv[])
{
return 0;
}
并运行以下命令:
$ clang -cc1 -fdump-record-layouts-simple simple.cpp
一无所获。 clang 版本是:
$ clang++ -dumpversion
4.2.1
如果我运行以下命令:
$ clang -cc1 --help
在很多信息中我可以找到这个:
...
-fdump-record-layouts-simple
Dump record layout information in a simple form used for testing
-fdump-record-layouts Dump record layout information
-fdump-vtable-layouts Dump the layouts of all vtables that will be emitted in a translation unit
...
我做错了什么?
最佳答案
您需要进行两项更改:
- 添加
-emit-llvm
编译器开关。没有这个,就不需要 LLVM 输出,因此记录布局永远不会被计算,因此不会被转储。 - 在您的代码中使用类。如果从未使用过这些类,则不会为它们生成代码,也不会转储它们的布局。
进行这些更改后,像这样的输出将打印在标准输出上:
*** Dumping AST Record Layout
Type: struct Base
Layout: <ASTRecordLayout
Size:32
DataSize:32
Alignment:32
FieldOffsets: [0]>
*** Dumping AST Record Layout
Type: struct Derived
Layout: <ASTRecordLayout
Size:64
DataSize:64
Alignment:32
FieldOffsets: [32]>
...
Layout: <CGRecordLayout
LLVMType:%struct.Base = type { i32 }
NonVirtualBaseLLVMType:%struct.Base = type { i32 }
IsZeroInitializable:1
BitFields:[
]>
Layout: <CGRecordLayout
LLVMType:%struct.Derived = type { %struct.Base, i32 }
NonVirtualBaseLLVMType:%struct.Derived = type { %struct.Base, i32 }
IsZeroInitializable:1
BitFields:[
]>
关于c++ - 转储 C++ 对象的内存布局在 clang 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51005713/