13

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?

Dman
  • 139
  • 1
  • 1
  • 5
  • 2
    What do you mean by .exe? Do you mean a Windows executable or a Linux executable? – TheJuice May 10 '10 at 15:44
  • 1
    To prevent further confusion, your solution is in sepp2k's answer. A .o file is an object file, it is the result of a compiled source file. You could say it's the intermediate file between the full executable and the source file. In order to "convert" (it's called "link") the (or multiple) object file to an executable, you have to push it through the linker program, in the case of gcc it's the -o flag. – LukeN May 10 '10 at 15:51
  • 2
    @LukeN: The -o flag does not affect what gcc does. It just allows you to give a name to the resulting executable - otherwise it will be called a.out (or something like a.out.exe on Windows, I suppose). – sepp2k May 10 '10 at 15:55
  • Didn't really think about what I wrote there, yes, -o is the output file, sorry about that :) – LukeN May 10 '10 at 15:57

2 Answers2

26
gcc foo.o -o foo.exe
sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 11
    This is the way... assuming that your .c had the main() function, that it does depend on other .c/.o files, and that it does not need to link to some non-standard/non-included library. – leonbloy May 10 '10 at 15:45
  • qgb.o: In function `__static_initialization_and_destruction_0(int, int)': /usr/include/c++/7/iostream:74: undefined reference to `std::ios_base::Init::Init()' – CS QGB Nov 01 '22 at 07:07
12

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.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • This might be over two years old, but why did you create the .o file the first time using `gcc -c hello.c -o hello.o` and then create the .o file the second time using `gcc -c -o main.o main.c` instead of `gcc -c main.c -o main.o`? – jII Jun 29 '12 at 11:16
  • @jesterII: The `hello.c` file demonstrates how standalone `.o` files can be created. The `main.c` file demonstrates how a standalone `.o` file can be converted to an `exe` file. The `hello.c` cannot be converted to a `exe` file without linking with another file containing the entry point. – Thomas Matthews Dec 13 '13 at 16:04