-1

what is faster? pre-compiled headers or same headers built to objects?

Question: Do I still need precompiled headers If I move to model above? Anything else you'd advise to speed up time to update?

I used pre-compiled headers and it seem to slightly speed up compilation.

In my case some part moved out from main app to decrease time to update project (compilation, building, execution. time)

I considering to build all neat, fully debugged functions to .so object files and link it accordingly to dependent apps

1
  • 2
    Headers are not inside .o object files. It's impossible to compare. same headers built to .so file objects Headers are also not inside a .so shared library. Commented Jan 15, 2024 at 11:05

2 Answers 2

2

Precompiled headers only cache header parsing. The parsed header still needs to be integrated into the rest of the C/C++ file and the result still needs to be optimized.

SO files are the UNIX flavor of dynamic libraries. Both static and dynamic libraries contained fully compiled and optimized functions that only need to be linked.

Linking finished external nontrivial functions will always be faster than expanding inline functions from (compiled or plaintext) header files.

Moving the contents of a header file to an object file/library doesn't really make sense, though. A normally used header file should only contain macros, type/type-alias definitions, inline functions, and, in C++, template definitions. None of that those generate linkable objects (external symbol definitions). Compiling a normal header file into an object file should always yield an empty object file.

Moving inlinable code from header files to C/C++ files (or at least isolating rarely used inline code away from a frequently included header files) while making it noninlinable is always a good idea as far as build times are concerned, but changing a function from inlinable to noninlinable can have noticeable runtime performance consequences where inlining is truly warranted (rare; usually very small functions only), so it shouldn't be done mindlessly.

Sign up to request clarification or add additional context in comments.

2 Comments

Petr, my apologies, I defo meant Class itself -> header and cpp files. Lets say I have 10 classes each class is 1 header and cpp for 1000 lines, I want to make .so file and compile it only once and reuse it from other app Please refer my previous question stackoverflow.com/questions/77815623/…
@J.Nowicky I don't think there was any confusion. I only used the term C/C++ for your implementation files (cpp files as you call them), as various file suffixes are used for C++ (cpp,C,cc,cxx) and the answer applies to C projects as well.
1

You're missing C++ modules (introduced with C++20)

C++ headers do not build to objects, so there is no comparison here which you can make.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.