Clang不支持在使用'-o'指定输出时直接添加头文件

错误信息

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

问题介绍

Clang不支持在使用'-o'指定输出文件时直接添加头文件,但允许在编译命令中使用预编译头文件,以减少编译时间。

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

生成预编译头文件的命令:

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

可以通过添加 '-include' 命令使用预编译头文件:

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

Clang会先检查test.h对应的预编译头文件是否存在;如果存在,则会使用对应的预编译头文件对test.h进行处理,否则,Clang会直接处理test.h的内容。

如果设法在Clang编译命令中保留头文件,则可以通过添加命令'-include'的方法使其通过编译。

官方参考文档:https://clang.llvm.org/docs/UsersManual.html

解决方案

避免在 Clang 的编译命令中直接添加头文件,或者按照上述方法使用预编译功能。