生成视图.obj文件的头

发布于 2024-08-27 09:20:21 字数 102 浏览 6 评论 0原文

有没有办法为 .obj 文件生成 C++ 头文件?或者也许有一个实用程序可以查看 .obj 文件。我已经找到了可以在格式之间进行转换的 objconv,但我找不到任何 .h 生成器/查看器。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜妞爱困 2024-09-03 09:20:21

考虑到 C++ 标签,情况并不像其他一些答案所暗示的那么绝望。

特别是,至少对于大多数 C++ 编译器来说,函数的名称将被修改(微软称其为“装饰”)以指示该函数所采用的参数。不同编译器的重整方案有所不同,但本质上它们都编码了足够的信息,以便您重新创建函数的声明——返回类型、函数本身的名称、类名(如果它是成员函数) ,以及函数期望的参数的确切数量和类型。

虽然不必通过修改名称来完成,但 C++ 系统别无选择,只能在目标文件中包含参数信息。当您重载函数时,链接器需要某种方法来确定哪个重载与特定调用链接。

我应该补充一点,将这些全部整理出来可能需要相当多的工作 - 例如,如果代码包含模板,则目标文件中的符号名称将展开所有模板参数(包括默认参数)。举个例子,考虑这样一段微不足道的代码:

#include <vector>
#include <string>

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

当我用 VC++ 编译它,然后执行 dumpbin /symbols foo.obj 时,输出约为 75 KB。尽管我的源代码似乎只定义了一个符号,但目标文件包含大约 180 个符号的定义,其中许多符号几乎与我编写的任何内容完全无关。

还值得一提的是,如果目标文件是使用调试信息进行编译的,那么(通常)也会在目标文件中产生更多的类型信息(等等)。

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.

夜雨飘雪 2024-09-03 09:20:21

您的问题带有 visual-c++ 标记,因此我假设您使用的是 Windows。

从目标文件生成原始头文件是不可能的,因为在将源代码编译到目标文件时,头文件中的许多(可以说是大多数)信息被删除。

但是,您可以使用 dumpbin 实用程序在非常低的级别检查对象文件。目标文件中存在的唯一您可能感兴趣的信息是给定目标文件中定义的符号名称列表。但您无法从目标文件中获取任何类型信息。因此,您无法判断给定函数接受或期望什么类型的值。

在某些情况下(当所有参数都通过堆栈传递给函数时),您可能可以知道函数所需的参数数量(通过反汇编函数代码的开头并计算从堆栈中弹出的值的数量)。但这在绝大多数情况下都行不通。

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.

伴随着你 2024-09-03 09:20:21

不可能有标头生成器,因为 .obj 文件不包含标头文件中所需的所有信息。至于转储 .obj,执行此操作的工具将取决于编译器。用于执行此操作的 GCC 相关工具是 objdump

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文