For a GCC or Clang command, there is typically one primary output
file, specified by -o or the default (a.out or
a.exe). There can also be temporary files and auxiliary
files.
Primary output file
We can specify the primary output file with the -o
option. When unspecified, a default output file name is inferred from
the input files and the final phase. When the final phase is linking,
the default output file name is a.out or
a.exe.
1 | gcc -S d/a.c |
For -S and -c, specifying -o
in the presence of more than one input files leads to an error.
1 | % gcc -c d/a.c d/b.c -o e/x |
Temporary files
For compilation and linking in one command, the relocatable object files are temporary files.
For GCC, when generating a relocatable object file, it needs to generate a temporary assembly file, then feeds it to GNU assembler.
We can set the temporary directory with one of the environment
variables TMPDIR, TMP, and TEMP.
When none is specified, compilers have a fallback, /tmp on
*NIX systems.
Auxiliary files
Beside primary output files and temporary output files, we have a
third output file type called auxiliary output files. Some options such
as -ftest-coverage and -gsplit-dwarf
cause the compiler to generate auxiliary output files.
For compilation without linking (-c, -S,
etc), the auxiliary output file names are derived from the primary
output file name.
For compilation and linking in one command, the primary output file name affects the auxiliary output file names.
1 | gcc -c -g -gsplit-dwarf d/a.c d/b.c |
GCC provides some options that are primarily of interest to GCC developers. These dump output files are treated the same way as auxiliary output files.
-dumpdir and -dumpbase are provided to
control the auxiliary output file names. The official
documentation may be difficult to follow. Let's see some
examples.
1 | gcc -g -gsplit-dwarf -dumpdir f d/a.c -c |
In the absence of -dumpdir, -dumpbase
appends a dash, which makes it inconvenient.
I suggest that you only use -dumpdir. When only
-dumpdir is used, the behavior is still easy to
explain.
I have a patch to support -dumpdir in Clang: https://reviews.llvm.org/D149193.
-save-temps
-save-temps
and -save-temps={cwd,obj} generate intermediate files,
which are treated as auxiliary output files in GCC.
1 | gcc -save-temps d/a.c |
In the absence of -dumpdir/-dumpbase,
-save-temps=cwd places intermediate files in the current
directory while -save-temps=obj places intermediate files
in the directory of the primary output file. -save-temps
behaves like -save-temps=obj when -o is
specified, and -save-temps=cwd otherwise.
When -dumpdir is specified, there is complex interaction
between -dumpdir and
-save-temps/-save-temps={cwd,obj}.
1 | # The last of -dumpdir and -save-temps wins. |
For Clang, I think we should abandon the idea to treat these
intermdiate files (*.i, *.bc,
*.s, etc) as auxiliary output files. Just make
-dumpdir and
-save-temps/-save-temps={cwd,obj} orthogonal.
1 | clang -g -gsplit-dwarf d/a.c -o e/x -save-temps=obj -dumpdir f/ |
Other auxiliary files
Clang supports a few options (e.g. -ftime-trace) to
generate other auxiliary output files. I plan to change their file names
to be controlled by -dumpdir.
Offloading
Clang supports offloading to various architectures using programming models like CUDA, HIP, and OpenMP. Using offloading options may generate multiple output files.
TODO