Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use .pch with clang?

Tags:

clang

pch

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
like image 274
4ntoine Avatar asked Nov 05 '14 10:11

4ntoine


1 Answers

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?

like image 180
4ntoine Avatar answered Sep 20 '22 17:09

4ntoine