Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an executable file from a c object file?

Tags:

c

object-files

How to make an object file to executable file?

like image 714
MalarN Avatar asked Dec 04 '09 13:12

MalarN


People also ask

Can an object file be executable?

An object file is a computer file containing object code, that is, machine code output of an assembler or compiler. The object code is usually relocatable, and not usually directly executable.

How do I open a .o file?

The best way to open an O file is to simply double-click it and let the default assoisated application open the file. If you are unable to open the file this way, it may be because you do not have the correct application associated with the extension to view or edit the O file.

What are .o files C?

Object file(.o): These files are produced as the output of the compiler. They consist of function definitions in binary form, but they are not executable by themselves and by convention their names end with .o. Binary executables file(.exe): These files are produced as the output of a program called a “linker“.


2 Answers

You need to link the object file. Your command:

gcc -c -o file.cgi file.c

compiles file.c into an object file (which would typically be called file.o). If you get rid of the '-c', it will generate the executable directly:

gcc -o file.cgi file.c

Alternatively (more useful if you have multiple files and don't want to compile all files when only one has changed), do it in two steps:

# Compile only
gcc -c -o file.o file.c
gcc -c -o file2.o file2.c
# Link
gcc -o file.cgi file.o file2.o
like image 55
DrAl Avatar answered Oct 13 '22 05:10

DrAl


If your file is a .o file that contains a main function you just need to link it, for example gcc file.o -o executable

like image 41
Arkaitz Jimenez Avatar answered Oct 13 '22 04:10

Arkaitz Jimenez