Compiler output files
2023-4-25 15:0:0 Author: maskray.me(查看原文) 阅读量:12 收藏

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
2
3
4
gcc -S d/a.c      
gcc -c d/a.c
gcc d/a.c
gcc d/a.c -o e/a

For -S and -c, specifying -o in the presence of more than one input files leads to an error.

1
2
3
% gcc -c d/a.c d/b.c -o e/x
gcc: fatal error: cannot specify ‘-o’ with ‘-c’, ‘-S’ or ‘-E’ with multiple files
compilation terminated.

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
2
3
4
gcc -c -g -gsplit-dwarf d/a.c d/b.c      
gcc -c -g -gsplit-dwarf d/a.c -o e/a.o
gcc -g -gsplit-dwarf d/a.c d/b.c -o e/x
gcc -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
2
3
4
5
6
7
8
gcc -g -gsplit-dwarf -dumpdir f d/a.c -c 
gcc -g -gsplit-dwarf -dumpdir f d/a.c
gcc -g -gsplit-dwarf -dumpdir f/ d/a.c
gcc -g -gsplit-dwarf -dumpbase f d/a.c
gcc -g -gsplit-dwarf -dumpbase f/ d/a.c


gcc -g -gsplit-dwarf -dumpdir f/ -dumpbase x d/a.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
2
gcc -save-temps d/a.c
ls a.i a.s a.o

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
2
3
# The last of -dumpdir and -save-temps wins.
gcc -g -gsplit-dwarf d/a.c -o e/x -dumpdir f/ -save-temps=obj # e/a.{i,s,o,dwo}
gcc -g -gsplit-dwarf d/a.c -o e/x -save-temps=obj -dumpdir f/ # f/a.{i,s,o,dwo}

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


文章来源: https://maskray.me/blog/2023-04-25-compiler-output-files
如有侵权请联系:admin#unsafe.sh