C++ 将 .o 文件转换为 .exe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2804044/
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-28 11:07:38  来源:igfitidea点击:

Convert .o file to .exe

c++cgccobject-files

提问by Dman

Is it possible to convert an object file .o that was created from a .c source code to .exe? And if it is possible is there a direct command using gcc?

是否可以将从 .c 源代码创建的目标文件 .o 转换为 .exe?如果可能的话,是否有使用 gcc 的直接命令?

回答by sepp2k

gcc foo.o -o foo.exe

回答by Thomas Matthews

Converting a .oto a .exemay be possible, depending on the contents of the .o. The .omust satisfy the requirements of an .exe. One of those is a mainfunction.

将 a 转换.o为 a.exe是可能的,具体取决于.o. 在.o必须满足的要求.exe。其中之一是main函数。

I commonly separate projects into pieces by theme. Each piece is translated into a .ofile. An individual piece cannot be converted to a .exe, but all the pieces combined can be converted.

我通常按​​主题将项目分成几部分。每一段都被翻译成一个.o文件。单个作品不能转换为.exe,但可以转换所有组合的作品。

For example, if I compile the following file it will turn into a .ofile:
{hello.c}

例如,如果我编译以下文件,它将变成一个.o文件:
{hello.c}

#include <stdio.h>

void Hello()
{
  puts("Hello");
  return;
}

Next, I compile:

接下来,我编译:

gcc -c hello.c -o hello.o

gcc -c hello.c -o hello.o

This will create the hello.ofile. This cannot be converted into a .exefile because it has no starting function. It is just information.

这将创建hello.o文件。这不能转换成.exe文件,因为它没有启动功能。它只是信息。

However, the following text can be converted from .oto .exe:
{main.c}

但是,以下文本可以转换.o.exe
{main.c}

#include <stdio.h>
int main(void)
{
  puts("Hello from main.\n");
  return 0;
}

Create a .ofile:

创建一个.o文件:

  gcc -c -o main.o main.c

And since it has an entry point, named mainby definition of the language, the main.ocanbe converted to a .exe:

由于它有一个入口点,main由语言定义命名,main.o可以转换为.exe

  gcc -o main.exe main.o

In summary, some .ofiles can be converted to .exewhile others can't. In the C and C++ languages, a .ofile must have a mainfunction in order to become an executable, .exefile. Note: The C and C++ language specifications do not require translation to .ofiles before creating an executable.

总之,有些.o文件可以转换为,.exe而有些则不能。在 C 和 C++ 语言中,.o文件必须具有main函数才能成为可执行.exe文件。 注意:C 和 C++ 语言规范.o在创建可执行文件之前不需要转换为文件。