4

I am working on a database project that compiles queries (expressed in some higher level language) into c++ code. This code is compiled and executed by the database. That part works perfectly fine.

Right now, I am trying to reduce the compile time for the C++ query code. I was wondering whether I can use precompiled headers to gain performance here.

The query is translated into a file called Query.cpp which includes library/src/Database.hpp. The Database.hpp file includes further files like StandardTypes.hpp and so on. Can I precompile all those header files to speed up the compilation of Query.cpp? If yes, how can I do that? I could not find any good example for precompiled headers so far, only some really basic stuff.

I use the following command to compile Query.cpp:

clang++ -fPIC -std=c++11 Query.cpp -I./library/src/ -shared -o libquery.so;
5gon12eder
  • 24,280
  • 5
  • 45
  • 92
moo
  • 486
  • 8
  • 22

1 Answers1

2

to create pre-compiled header include all the headers you don't change into Query.h and use:

clang -cc1 Query.h -emit-pch -o Query.h.pch

to use the pre-compiled header type:

clang -cc1 -include-pch Query.h.pch Query.cpp -shared -o libquery.so;

Query.cpp needs to include Query.h

codekiddy
  • 5,897
  • 9
  • 50
  • 80
  • Good idea, I will give it a try. Is there any chance to see whether clang used the precompiled header or not? What's about the flags: -shared -fPIC -std=c++11? – moo Mar 05 '15 at 01:01
  • Not sure but you can append the **-v** flag which will show verbose output. – codekiddy Mar 05 '15 at 01:06
  • But actually it would also be sufficient to precompile the Database.hpp header, or? It is the only header that is directly included by Query.cpp – moo Mar 05 '15 at 01:08
  • Only one pre-compiled header can be used. you can include any amount of headers into that one header, -std=c++11 is OK, -fPIC is used for shared libraries, and executables, and indicates that executables won't be dependent on fixed startup address. – codekiddy Mar 05 '15 at 01:11
  • I think Also each of the source files need to include precompiled header, the one with *.h extension, not *.h.pch – codekiddy Mar 05 '15 at 01:12
  • I know what the flags mean, but I was wondering whether I have to add them during header compilation? – moo Mar 05 '15 at 01:13
  • No you don't have to add -fPIC for precompiled header since the command will not create executables, std=c++11 is needed only if headers contain C++11 code. but even if it does not it won't hurt. – codekiddy Mar 05 '15 at 01:14
  • The `emit-pch` flag was causing an odd warning on clang-3.8, but it appears to still work if I remove it. – Jason Rice Dec 03 '15 at 20:40
  • In recent version of `clang`, it is no longer necessary to use the `-cc1` option to use PCH. See the question [How do I generate and use precompiled headers with Clang++?](https://stackoverflow.com/questions/55885920/how-do-i-generate-and-use-precompiled-headers-with-clang). – Scott McPeak Jun 13 '22 at 12:08