recompile after base class change

痞子三分冷 提交于 2019-12-02 06:43:41

问题


In particular, the way most C++ implementations work implies that a change in the size of a base class requires a recompilation of all derived classes.

This statement is from stroustrup book. So if base class is in a .so file and we just change a member function implementation, then does it mean that we don't have to recompile my program linked to that shared object?


回答1:


Formally, if you don't recompile you're violating the One Definition Rule, and get undefined behavior.

Practically, as long as the member function you modify hasn't been inlined anywhere, and you aren't changing the signature, you probably retain binary compatibility. On most platforms. If you're lucky, your platform documentation provides such a guarantee.




回答2:


I believe your understanding is correct. Just changing the body of a member function doesn't change the amount of space required for an instance of that object. The code isn't stored "in" the object instance; only the data is.

When the class is compiled, references to member data fields are just offsets from the beginning of that object data. And derived classes' data are typically placed after the base class's data. So if you add a field to the base class, the correct offsets for the derived class data have all changed, which implies the base class needs recompiled, to point to the new (correct) offsets.

Before

class Foo {
    int a;              // offset 0 (assuming no vtable)
}

class Bar : public Foo {
    int b;              // offset 4
}

Bar bar;  bar.b = 7;    // sets the 32-bit value at this+4 to 7

After

class Foo {
    int a;              // offset 0
    int c;              // offset 4
}

class Bar : public Foo {
    int b;              // offset 8
}

Bar b;   bar.b = 7;     // Without recompiling: sets the 32-bit value at this+4
                        //   which is actually where Foo.a is stored!
                        // With recompiling: sets the 32-bit value at this+8



回答3:


If it just the implementation, it should work fine. That's whole concept of Windows DLL's. Adding or removing interfaces won't changes the class size (unless you're introducing a new virtual function) but in the large extend the way functions laid out in the memory could be changed. So a recompilation is required if you're making use of the new function. On the other hand, most of the modern compilers are smart enough to identify the relevant changes because of a simple modification in the header files.



来源:https://stackoverflow.com/questions/9188101/recompile-after-base-class-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!