cgccobject-files

C programming basics: Why can't I see the .o file after I compile my .c file with gcc


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.


Solution

  • 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