如何使库独立于编译器版本? [英] how to make libraries independent to compiler version?

查看:78
本文介绍了如何使库独立于编译器版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在使用msvc编译器

I use msvc compiler now

我想制作独立于mvsvc版本的链接库(lib,dll).

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

是否可以制作独立的库?

Is it possible to make the independent libraries?

推荐答案

静态库-否. DLL-是的,只要适当设计了公共接口.

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

通常有两种方法可以通过DLL来实现:

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

  1. DLL导出许多形成C样式API的函数,类似于Windows API.这些函数仅在其签名中使用原始类型及其数组和结构(无C ++类).对象由句柄(参见HWNDHDC)表示,并带有一组用于操纵这些句柄的函数.

  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.

DLL是COM服务器(或至少导出类似COM的接口).基本上,工厂函数(在COM中,DllGetClassObject)制造指向没有数据成员且所有成员函数均为纯虚函数(通常称为接口)的抽象类的指针.所有访问都是通过在这些接口上调用方法(这些方法又可以制造其他接口指针)来实现的.

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).

您还应注意的另一个考虑因素是资源管理-特别是但不限于内存管理.您不能假定DLL中的malloc内存可以在客户端中free d进行存储,反之亦然.您必须确保资源分配/取消分配永远不会跨越模块边界. DLL可能永远不会分配客户端需要释放的内存.或DLL导出了客户端应调用的特殊功能,以释放DLL分配的内存;或DLL使用了操作系统提供的功能(例如,COM中的CoTaskMemAlloc/CoTaskMemFree).

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).

这篇关于如何使库独立于编译器版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆