How to use .pch with clang?
Asked Answered
B

1

7

I'm having header file header.h:

#define TEST_VALUE 1
#define TEST_STRING "hello world"

and source file source.cpp:

#include "header.h"
#include "stdio.h"

int main() {
    printf(TEST_STRING"\n");
}

I've followed clang pch article and performed:

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

So i can't link with PCH but i can compile/link source+header. The only difference with clang article's example is i've tried c++ and it's described for c. So i've tried to specify c++ language:

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

One more attempt with specifying 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'm not trying to add -I since i'm able to compile/link with clang -I. source.cpp -o source and include paths are (should be) the same.

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
Bounded answered 5/11, 2014 at 10:43 Comment(5)
hm, shouldn't it be clang++ and #include <stdio.h> or #include <cstdio> ?Venuti
i've just tried both compiling the header and link with clang++ and with cstdio - no luck. getting the same errorBounded
First of all, do not use -cc1 this way - just compare your command line and clang -### -c -o test.cc for any custom test.cc. There is a lot of missing internal flags.Aptitude
I've got this command-line from clang's tutorial (see link in the Question). What is correct command-line to generate/use PCHs?Bounded
@Bounded try to extract command-line from clang -### -c -o test.cc and add there the essential flag -include-pch. Or try your short command-line without -cc1 at all.Aptitude
B
8

I've solved it:

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++

BTW, if i rename header.h and try to link executable, i get error:

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.

It means it still needs header file! Is it expected behaviour?

PS2. I've tried to add one more header to .pch and failed:

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

Created separate SO question about it: How to generate .pch for lots of headers?

Bounded answered 5/11, 2014 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.