Rate This Document
Findability
Accuracy
Completeness
Readability

Clang Does Not Allow a Header File to Be Added Directly When -o Is Used to Specify the Output

Error Information

1
2
clang test.h test.c -o test 
clang-10: error: cannot specify -o when generating multiple output files

Problem

Clang does not allow a header file to be added directly when -o is used to specify the output file. However, a precompiled header file can be used in compile commands to reduce compilation time.

1
2
$ cat test.c  
#include "test.h"

Run the following command to generate a precompiled header file:

1
clang -x c-header test.h -o test.h.pch 

You can add the following -include command to use the precompiled header file:

1
clang -include test.h test.c -o test

Clang first checks whether the precompiled header file corresponding to test.h exists. If yes, Clang uses the corresponding precompiled header file to process test.h. Otherwise, Clang directly processes test.h.

If a header file needs to be retained in Clang compile commands, you can add the -include command to compile the file.

For details, see the official reference document at https://clang.llvm.org/docs/UsersManual.html.

Solution

Do not directly add a header file in Clang compile commands. Alternatively, use the aforementioned precompilation function.