C++ *.o 文件是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2186246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:27:28  来源:igfitidea点击:

What is *.o file?

c++

提问by Max Frai

I'm compiling own project. And it halted by this error:

我正在编译自己的项目。它因这个错误而停止:

LINK||fatal error LNK1181: cannot open input file 'obj\win\release\src\lua\bindings.o'|

LINK||致命错误 LNK1181:无法打开输入文件 'obj\win\release\src\lua\bindings.o'|

Compiling using Code::Blocks with VS 2005/2008 compiler under win7. There are also lot of another empty directories where *.o files are missing.

在win7下使用VS 2005/2008编译器使用Code::Blocks进行编译。还有很多其他空目录缺少 *.o 文件。

What do they do?

他们在做什么?

回答by Joseph Salisbury

A file ending in .o is an object file. The compiler creates an object file for each source file, before linking them together, into the final executable.

以 .o 结尾的文件是目标文件。编译器为每个源文件创建一个目标文件,然后将它们链接在一起,成为最终的可执行文件。

回答by Jerry Coffin

You've gotten some answers, and most of them are correct, but miss what (I think) is probably the point here.

你已经得到了一些答案,其中大部分是正确的,但错过了(我认为)可能是这里的重点。

My guess is that you have a makefile you're trying to use to create an executable. In case you're not familiar with them, makefiles list dependencies between files. For a really simple case, it might have something like:

我的猜测是您有一个要用于创建可执行文件的 makefile。如果您不熟悉它们,makefile 会列出文件之间的依赖关系。对于一个非常简单的案例,它可能是这样的:

myprogram.exe: myprogram.o
    $(CC) -o myprogram.exe myprogram.o

myprogram.o: myprogram.cpp
    $(CC) -c myprogram.cpp

The first line says that myprogram.exedepends on myprogram.o. The second line tells how to create myprogram.exefrommyprogram.o. The third and fourth lines say myprogram.odepends on myprogram.cpp, and how to create myprogram.ofrom myprogram.cpp` respectively.

第一行说这myprogram.exe取决于myprogram.o. 第二行说明如何myprogram.exemyprogram.o. 第三行和第四行分别说myprogram.o取决于myprogram.cpp,以及如何myprogram.o从 myprogram.cpp`创建。

My guess is that in your case, you have a makefile like the one above that was created for gcc. The problem you're running into is that you're using it with MS VC instead of gcc. As it happens, MS VC uses ".obj" as the extension for its object files instead of ".o".

我的猜测是,在您的情况下,您有一个类似于上面为 gcc 创建的 makefile。您遇到的问题是您将它与 MS VC 而不是 gcc 一起使用。碰巧的是,MS VC 使用“.obj”作为其目标文件的扩展名,而不是“.o”。

That means when make (or its equivalent built into the IDE in your case) tries to build the program, it looks at those lines to try to figure out how to build myprogram.exe. To do that, it sees that it needs to build myprogram.o, so it looks for the rule that tells it how to build myprogram.o. That says it should compile the .cpp file, so it does that.

这意味着当 make(或在您的情况下内置于 IDE 中的等效项)尝试构建程序时,它会查看这些行以尝试找出如何构建myprogram.exe. 为此,它看到它需要 build myprogram.o,因此它会查找告诉它如何 build 的规则myprogram.o。也就是说它应该编译 .cpp 文件,所以它会这样做。

Then things break down -- the VC++ compiler produces myprogram.objinstead of myprogram.oas the object file, so when it tries to go to the next step to produce myprogram.exefrom myprogram.o, it finds that its attempt at creating myprogram.osimply failed. It did what the rule said to do, but that didn't produce myprogram.oas promised. It doesn't know what to do, so it quits and give you an error message.

然后事情就崩溃了——VC++ 编译器生成myprogram.obj而不是myprogram.o作为目标文件,所以当它试图进入下一步生成myprogram.exefrom 时myprogram.o,它发现它的创建尝试myprogram.o失败了。它做到了规则所说的那样,但并没有myprogram.o像承诺的那样产生。它不知道该怎么做,所以它退出并给你一条错误信息。

The cure for that specificproblem is probably pretty simple: edit the make file so all the object files have an extension of .objinstead of .o. There's room for a lot of question whether that will fix everything though -- that may be all you need, or it may simply lead to other (probably more difficult) problems.

解决该特定问题的方法可能非常简单:编辑 make 文件,使所有目标文件的扩展名都不.obj.o. 不过,这是否能解决所有问题仍有很多疑问——这可能就是您所需要的,或者它可能只会导致其他(可能更困难)的问题。

回答by Jerry Coffin

A .o object file file (also .obj on Windows) contains compiled object code (that is, machine code produced by your C or C++ compiler), together with the names of the functions and other objects the file contains. Object files are processed by the linkerto produce the final executable. If your build process has not produced these files, there is probably something wrong with your makefile/project files.

.o 目标文件文件(在 Windows 上也是 .obj)包含已编译的目标代码(即由 C 或 C++ 编译器生成的机器代码),以及文件包含的函数和其他对象的名称。目标文件由链接器处理以生成最终的可执行文件。如果您的构建过程没有生成这些文件,则您的 makefile/项目文件可能有问题。

回答by Cyrus Vahidi

It is important to note that object filesare assembled to binary code in a format that is relocatable. This is a form which allows the assembled code to be loaded anywhere into memory for use with other programs by a linker.

请务必注意,目标文件可重定位的格式组装为二进制代码。这种形式允许将汇编代码加载到内存中的任何位置,以便链接器与其他程序一起使用。

Instructions that refer to labels will not yet have an address assigned for these labels in the .ofile.

引用标签的指令尚未在.o文件中为这些标签分配地址。

These labels will be written as '0' and the assembler creates a relocation record for these unknown addresses. When the file is linked and output to an executable the unknown addresses are resolved and the program can be executed.

这些标签将被写为“0”,并且汇编器会为这些未知地址创建一个重定位记录。当文件被链接并输出到可执行文件时,未知地址被解析并且程序可以被执行。

You can use the nmtool on an object file to list the symbols defined in a .o file.

您可以对目标文件使用nm工具来列出 .o 文件中定义的符号。

回答by Frank V

Ink-Jet is right. More specifically, an .o (.obj) -- or object fileis a single source file compiled in to machine code (I'm not sure if the "machine code" is the same or similar to an executable machine code). Ultimately, it's an intermediate between an executable program and plain-text source file.

喷墨是对的。更具体地说,.o (.obj) - 或目标文件是编译成机器代码的单个源文件(我不确定“机器代码”是否与可执行机器代码相同或相似)。最终,它是可执行程序和纯文本源文件之间的中间体。

The linker uses the o files to assemble the file executable.

链接器使用 o 文件来组装文件可执行文件。

Wikipediamay have more detailed information. I'm not sure how much info you'd like or need.

维基百科可能有更详细的信息。我不确定您想要或需要多少信息。