开发者

question about recompiling the library in C++

开发者 https://www.devze.com 2022-12-20 23:23 出处:网络
Suppose my class is depending on other library. Now I need to modify the class for one application. What kind of modification will force me to recompile

Suppose my class is depending on other library. Now I need to modify the class for one application. What kind of modification will force me to recompile all libraries. What's the rule to recompile all libraries?

for example, I only know the case 2) is like this. What about the others?

1) add a constructor

2) add a data member

3) change destructor into virtual

4) add an 开发者_StackOverflowargument with default value to an existing member function


Do you really mean that the class you are changing depends on the library? You never have to recompile a library because you've changed something that depends on the library. You recompile a library if you change something that the library depends on.

The answer is that in C++, technically all of those things require recompiling anything that uses the class. The one definition rule only permits classes to be defined in multiple translation units if the definitions are exactly the same in all units (I think "exactly" means the same sequence of tokens after preprocessing, in which case even changing the name of a parameter requires recompilation). So if different source files share a header, and a class definition in that header changes, C++ guarantees nothing about whether code compiled from those two source files remains compatible if only one of them is rebuilt.

However, your particular C++ implementation will use a static/dynamic library format which relaxes the rules, and allows some changes to be "binary compatible". Of the things you list, only (1) has much chance of being binary compatible. You'd have to check your documentation, but it's probably fine. In general (2) changes the size and layout of the objects, (3) changes the code required by the caller to destroy objects, and (4) changes the signature of the function (default values are inserted by the calling code, not the callee).

It's often worth avoiding default parameters pretty much for this reason. Just add another overload. So instead of changing:

void foo(int a);

to

void foo(int a, int b = 0);

replace it with:

void foo(int a) { foo(a, 0); }
void foo(int a, int b);

Of course the former change isn't even source-compatible if a user is taking a pointer to the function foo, let alone binary compatible. The latter is source-compatible provided that the ambiguity is resolved over which foo to use. C++ does make some effort to help with this, initializing a function pointer is a rare (only?) case where context affects the value of an expression.


You'll only need to recompile the code that depends on your class (like Nikolai said), i.e. you're class resides in a library that's used by others. Even then you'll need to recompile the dependent code only if your class:

  1. changes it's memory layout:
    • you add/remove members;
    • change members types;
    • you add a virtual method or change an existing one to be virtual (so you add a hidden vpointer member);
  2. changes method signatures (more precisely changes in what the compiler uses for name decoration/mangling):
    • change their constness;
    • change their virtualness;
    • add/change default parameter values.

I'm pretty sure I've missed some things but I'll add any other things that come up (either from comments or if my memory starts working better).


If you're only using those libraries, nothing forces you to recompile them ...

except changing of compiler / architecture / OS or changing some #defines of the libraries


The question is a bit confusing, so to get this strait, you only need to recompile code that depends on your class.

Then for things that depend on your class:

  1. Adding a constructor to your class introduces a new function, so if client code doesn't use that constructor - no recompilation is needed,
  2. Adding data member changes memory layout of the class - recompilation is required,
  3. Changing destructor to virtual changes/introduces vtable layout and function dispatch code - recompilation is required,
  4. Adding argument with default value to existing member function changes number of arguments to that function (default arguments are substituted at the call site) - client code recompilation is required.


Changing any source code or classes that the library depends on should force a recompilation of the library. A good build tool, with the dependencies set up correctly, will handle this automagically during the build process.

0

精彩评论

暂无评论...
验证码 换一张
取 消