Patrick
Patrick

Reputation: 321

Where are object files of c programs stored?

After a c source code is compiled, an object file is created. I can only see the .exe file so where can I find the object file? I am using cygwin on windows so I am using gcc to compile programs.

Upvotes: 3

Views: 1881

Answers (3)

It is implementation specific. Some (weird) implementations of C are interpreters, or (e.g. tinycc) may compile in memory (so don't produce any files). Others (e.g. my GCC compiler, when not asked to...) don't keep object files (so when you gcc -Wall foo.c bar.c -o prog.exe the foo.obj & bar.obj are temporary so are not kept; but YMMV).

Upvotes: 0

Jens
Jens

Reputation: 72697

Many compilers support the -c option to compile only and produce object files.

If you use a make-based build, object files may be treated as intermediate and automatically removed. You may declare them in the .PRECIOUS: target to avoid this.

If your program consists of only a single C source file, you could use

gcc -c file.c       # Creates file.o, but no file.exe
gcc -o prog file.o  # Creates prog.exe by linking file.o with appropriate libs

Upvotes: 7

nalzok
nalzok

Reputation: 16127

As a language-lawyer answer, because the standard doesn't specify an "object file" must be created when a source is being compiled:(Quotation from N1570, 6th footnote, emphasis mine)

6) Implementations shall behave as if these separate phases occur, even though many are typically folded together in practice. Source files, translation units, and translated translation units need not necessarily be stored as files, nor need there be any one-to-one correspondence between these entities and any external representation. The description is conceptual only, and does not specify any particular implementation.

However, note that on most main-stream implementations, "object files" are generated and deleted. If you want to keep them, add the -c flag (works on both GCC and clang).

Upvotes: 2

Related Questions