2018年总结

Read more →

DLL的二进制兼容

详解 什么是二进制兼容? 所谓二进制兼容就是在做版本升级(也可能是Bug fix)库文件的时候,不必要重新编译使用这个库的可执行文件或使用这个库的其他库文件,同时能保证程序功能不被破坏。 当然,这只是一个现象级描述,其实在一些简单的例子里,假设我们导出一个C++类,在调用时,第三方仍然不需要重新编译可以运行。如下面例子: FastString.dll - FastString.h文件 //导出类 class __declspec(dllexport) FastString { public: FastString(); ~FastString(); size_t length() { return 0; } private: unsigned char *m_bytes; } test.exe - main.cpp文件 int main() { FastString fStr; size_t len = fStr.length(); printf("fast string length %d\n", len); _getch(); return 0; } 如果我们给导出类加上一个虚函数 virtual boole isEmpty(); // 位于 length 方法之前 重新编译FastString.dll,然后直接运行test.exe,发现仍然能打印出fast string length 0,并且没有运行错误。 所以按照上面所说,FastString.dll是二进制兼容的。然而不是的!因为它增加了一个虚函数,导致FastString实例增加了一个虚函数表(是一个void **指针),那为什么运行的时候没有错误呢?参考这个问题:SO- why new virtual function will not break binary compatibility per phenomenon?
Read more →