how to make libraries independent to compiler version?

倖福魔咒の 提交于 2019-12-12 22:24:35

问题


I use msvc compiler now

I want to make linking libraries (lib, dll) which independen to the mvsvc version..

Is it possible to make the independent libraries?


回答1:


Static libraries - no. DLLs - yes, when the public interface is designed appropriately.

There are generally two ways to achieve that with a DLL:

  1. The DLL exports a number of functions forming a C-style API, similar to Windows API. Those functions use only primitive types in their signature, and arrays and structures thereof (no C++ classes). Objects are represented by handles (cf. HWND or HDC) with a set of functions for manipulating those handles.

  2. The DLL is a COM server (or at least exports a COM-like interface). Basically, a factory function (in COM, DllGetClassObject) that manufactures a pointer to an abstract class with no data members and all member functions pure virtual (commonly referred to as an interface). All access is by calling methods on these interfaces (which may in turn manufacture other interface pointers).

Another consideration you should pay attention to is resource management - in particular, but not limited to, memory management. You cannot assume that a memory malloced in the DLL could be freed in the client, or vice versa. You must ensure that resource allocation/deallocation never crosses module boundaries. It could be that the DLL never allocates memory that the client needs to free; or that the DLL exports a special function that the client should call to deallocate memory that the DLL allocated; or that the DLL uses facilities provided by the operating system (e.g. CoTaskMemAlloc / CoTaskMemFree in COM).



来源:https://stackoverflow.com/questions/21885909/how-to-make-libraries-independent-to-compiler-version

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