【问题标题】:How to use .pch with clang?如何将 .pch 与 clang 一起使用?
【发布时间】: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

标签: clang pch


【解决方案1】:

我已经解决了:

MBA-Anton:pch asmirnov$ clang++ -x c++-header header.h -emit-pch -o header.pch
MBA-Anton:pch asmirnov$ clang++ -include-pch header.pch source.cpp -o source -x c++

顺便说一句,如果我重命名 header.h 并尝试链接可执行文件,我会收到错误:

MBA-Anton:pch asmirnov$ clang++ -include-pch header.pch source.cpp -o source -x c++
fatal error: malformed or corrupted AST file: 'could not find file '/tmp/pch/header.h' referenced by
      AST file'
1 error generated.

这意味着它仍然需要头文件!这是预期的行为吗?

PS2。我尝试在 .pch 中再添加一个标头,但失败了:

MBA-Anton:pch asmirnov$ clang++ -x c++-header header.h header2.h -emit-pch -o headers.pch
clang: error: cannot specify -o when generating multiple output files

为此创建了单独的 SO 问题: How to generate .pch for lots of headers?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 2011-06-08
    • 2016-08-07
    • 1970-01-01
    相关资源
    最近更新 更多