【发布时间】:2014-11-06 18:13:55
【问题描述】:
我有头文件 header.h:
#define TEST_VALUE 1
#define TEST_STRING "hello world"
和源文件source.cpp:
#include "header.h"
#include "stdio.h"
int main() {
printf(TEST_STRING"\n");
}
我已经关注clang pch article并执行了:
MBA-Anton:pch asmirnov$ clang -cc1 ./header.h -emit-pch -o ./header.pch
MBA-Anton:pch asmirnov$ clang -cc1 -include-pch header.pch source.cpp -o source
error: C99 was enabled in PCH file but is currently disabled
1 error generated.
MBA-Anton:pch asmirnov$ clang -I. source.cpp -o source
MBA-Anton:pch asmirnov$ ls
header.h header.pch source source.cpp
MBA-Anton:pch asmirnov$ ./source
hello world
所以我不能与 PCH 链接,但我可以编译/链接源 + 头文件。 与 clang 文章示例的唯一区别是我尝试过 c++ 并且它是针对 c 描述的。 所以我尝试指定 c++ 语言:
MBA-Anton:pch asmirnov$ clang -cc1 ./header.h -emit-pch -o ./header.pch2 -x c++
MBA-Anton:pch asmirnov$ clang -cc1 -include-pch header.pch2 source.cpp -o source2 -x c++
source.cpp:2:10: fatal error: 'stdio.h' file not found
#include "stdio.h"
^
1 error generated.
MBA-Anton:pch asmirnov$ ls -l
total 496
-rw-r--r-- 1 asmirnov wheel 56 5 ноя 15:31 header.h
-rw-r--r-- 1 asmirnov wheel 112564 5 ноя 15:50 header.pch
-rw-r--r-- 1 asmirnov wheel 116236 5 ноя 15:50 header.pch2
-rwxr-xr-x 1 asmirnov wheel 8456 5 ноя 15:42 source
-rw-r--r-- 1 asmirnov wheel 80 5 ноя 15:32 source.cpp
再次尝试指定 c++11:
MBA-Anton:pch asmirnov$ clang -cc1 ./header.h -emit-pch -o ./header.pch3 -x c++ -std=c++11
MBA-Anton:pch asmirnov$ clang -cc1 -include-pch header.pch3 source.cpp -o source3 -x c++ -std=c++11
source.cpp:2:10: fatal error: 'stdio.h' file not found
#include "stdio.h"
^
1 error generated.
MBA-Anton:pch asmirnov$ ls -l
total 728
-rw-r--r-- 1 asmirnov wheel 56 5 ноя 15:31 header.h
-rw-r--r-- 1 asmirnov wheel 112564 5 ноя 15:50 header.pch
-rw-r--r-- 1 asmirnov wheel 116236 5 ноя 15:54 header.pch2
-rw-r--r-- 1 asmirnov wheel 117132 5 ноя 15:57 header.pch3
-rwxr-xr-x 1 asmirnov wheel 8456 5 ноя 15:42 source
-rw-r--r-- 1 asmirnov wheel 80 5 ноя 15:57 source.cpp
我没有尝试添加 -I,因为我可以使用 clang -I. source.cpp -o source 进行编译/链接,并且包含路径(应该)相同。
PS。
MBA-Anton:pch asmirnov$ clang --version
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
【问题讨论】:
-
嗯,不应该是
clang++和#include <stdio.h>还是#include <cstdio>? -
我刚刚尝试使用
clang++和cstdio编译标题和链接 - 不走运。得到同样的错误 -
首先,不要以这种方式使用
-cc1- 只需比较您的命令行和clang -### -c -o test.cc即可获得任何自定义test.cc。缺少很多内部标志。 -
我从 clang 的教程中得到了这个命令行(参见问题中的链接)。生成/使用 PCH 的正确命令行是什么?
-
@4ntoine 尝试从
clang -### -c -o test.cc提取命令行并在其中添加基本标志-include-pch。或者试试你的短命令行,根本不用-cc1。