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
Convert .o file to .exe
提问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 .o
to a .exe
may be possible, depending on the contents of the .o
. The .o
must satisfy the requirements of an .exe
. One of those is a main
function.
将 a 转换.o
为 a.exe
是可能的,具体取决于.o
. 在.o
必须满足的要求.exe
。其中之一是main
函数。
I commonly separate projects into pieces by theme. Each piece is translated into a .o
file. 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 .o
file:
{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.o
file. This cannot be converted into a .exe
file because it has no starting function. It is just information.
这将创建hello.o
文件。这不能转换成.exe
文件,因为它没有启动功能。它只是信息。
However, the following text can be converted from .o
to .exe
:
{main.c}
但是,以下文本可以转换.o
为.exe
:
{main.c}
#include <stdio.h>
int main(void)
{
puts("Hello from main.\n");
return 0;
}
Create a .o
file:
创建一个.o
文件:
gcc -c -o main.o main.c
And since it has an entry point, named main
by definition of the language, the main.o
canbe converted to a .exe
:
由于它有一个入口点,main
由语言定义命名,main.o
可以转换为.exe
:
gcc -o main.exe main.o
In summary, some .o
files can be converted to .exe
while others can't. In the C and C++ languages, a .o
file must have a main
function in order to become an executable, .exe
file. Note: The C and C++ language specifications do not require translation to .o
files before creating an executable.
总之,有些.o
文件可以转换为,.exe
而有些则不能。在 C 和 C++ 语言中,.o
文件必须具有main
函数才能成为可执行.exe
文件。 注意:C 和 C++ 语言规范.o
在创建可执行文件之前不需要转换为文件。