我要评分
获取效率
正确性
完整性
易理解

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

Error Information

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.

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

Run the following command to generate a precompiled header file:

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

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

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.