Due to the recent company to develop a C + + plug-in mechanism-based, there is a major problem is C + + binary compatibility issues.
Once the class uses the virtual function, as long as the random changes and additions and deletions to check the virtual function of the head file, it will cause the program to jump when running, because this time EXE and DLL inside the vtable model is inconsistent
Just the program is developed using QT, so there are two ways to ensure that the header file is consistent in order to load the program
1. Using the MOC mechanism of QT
The MOC mechanism of QT has a Q_invokable macro definition that allows MOC to generate information about the class member functions,
Then use the method inside the qobject to get the corresponding function parameter and function order.
Used to guarantee the order of virtual functions, parameters are consistent
See the Qobject documentation for specific information
Http://doc.qt.io/qt-5/qobject.html
Http://doc.qt.io/qt-5/qmetaobject.html
Http://doc.qt.io/qt-5/qmetamethod.html
Get the specified function information from the Qobject or MetaObject and get the Metamethod
2. Traverse the vtable to ensure that the number of virtual functions is consistent. How to get vtable on the web there are many articles, the following is mainly for VS2015 compiler code
The following code is the main thing to do:
1. Ensure that the incoming program has a virtual function
2. Guaranteed t no pointer type
3. Get vtable
4. Traverse vtable has been encountering 0
template <
class
T>
int
vtableLength(T &object)
{
static_assert
(std::is_polymorphic<T>::value,
"at least has one virtual function"
);
static_assert
(!std::is_pointer<T>::value,
"type not allow pointer"
);
int
* vptr = (
int
*)&object;
int
* vtable = (
int
*)*vptr;
int
len = 0;
for
(
int
i = 0; vtable[i] != 0; i++)
{
len++;
}
return
len;
}
|
Https://www.cnblogs.com/linyilong3/p/6750516.html
Qt with VC + + to carry out the verification mechanism of the plug-in (traverse vtable, to ensure that the number of virtual functions consistent, you can also use the Q_invokable macro definition)