Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert .o file to .exe

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?

like image 973
Dman Avatar asked May 10 '10 15:05

Dman


People also ask

Are .O files executable?

O files themselves are typically not executable. When compiling a C program, compilers first transform all the program's source code files into compiled object files. These compiled files typically use the .o or . OBJ extension.

What is the point of .o files?

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 linker to produce the final executable.

What program allows you to combine objects into an executable?

In computing, a linker or link editor is a computer system program that takes one or more object files (generated by a compiler or an assembler) and combines them into a single executable file, library file, or another "object" file.


2 Answers

gcc foo.o -o foo.exe
like image 112
sepp2k Avatar answered Oct 01 '22 12:10

sepp2k


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.

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.

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

#include <stdio.h>

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

Next, I compile:

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.

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

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

Create a .o file:

  gcc -c -o main.o main.c

And since it has an entry point, named main by definition of the language, the main.o can be converted to a .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.

like image 28
Thomas Matthews Avatar answered Oct 01 '22 11:10

Thomas Matthews