-2

I wrote a C programm and saved it with a .c extension. Then I compiled with the gcc but after that I only see my .c file and an .exe file. The program runs perfectly. But where is the .o file that I learned in theory? Has it been overwritten to .exe and all done by the gcc in on step? (Preprocessing, compiling, assembling and linking)

I'm on a VM running Debian.

Marsstar
  • 159
  • 4
  • 10

4 Answers4

2

By default, gcc compiles and links in one step. To get a .o file, you need to compile without linking. That's done with the -c option.

Suppose you want to compile two files separately, then link them. You would do the following:

gcc -c file1.c      # creates file1.o
gcc -c file2.c      # creates file2.o
gcc -o myexe file1.o file2.o

If you want just the output of the preprocessor, use the -E option along with the -o to specify the output file:

gcc -E file1.c -o file1-pp.c    # creates file1-pp.c
dbush
  • 205,898
  • 23
  • 218
  • 273
1

Compile and link in two steps:

gcc -Wall -c tst.c
gcc tst.c -o tst

After first command you'll get a .o file.

ouah
  • 142,963
  • 15
  • 272
  • 331
1

if you did something like gcc test.c then it produces only the executable file (in order to compile only, see the -c option)

ewcz
  • 12,819
  • 1
  • 25
  • 47
0

here is steps on compiling with gcc to create a .o file from your C file:

http://www.gnu.org/software/libtool/manual/html_node/Creating-object-files.html

httpNick
  • 2,524
  • 1
  • 22
  • 34
  • yes thanks, gcc -c <...> worked. But why does the command gcc -o don't create the object file and the .exe file? Does it delete it after it has linked it? Does the command (.o) do everything at once? – Marsstar Sep 24 '15 at 20:18