开发者

Linking to the correct library

I dont think my question here was answered here already. So here it is, I have a static library that I maintain and update periodically. I also update the version number correctly. Now my application which uses this library must link to exactly the same version of the library and not to the old开发者_如何学Pythoner or newer ones. One thing I tried was to use predefined macros in the libraries header file and check it in my application. It worked, but in that way you can only ensure the correct header files. Is there any technique so that the linking will fail if it is the wrong library?? I hope the question is somewhat clear.

Hari


All you need is a unique symbol related to the version number for example:

int version_1_1_5=0;

in the library and some use of it in the application, for example:

extern int version_1_1_5; //decl
int *p = &version_1_1_5;  // use


I'm not sure that I'd recommend it, but...

The header could include:

#define LIB_VERSION_SUFFIX _3_17

#define LIB_PASTER(x, y)    x ## y
#define LIB_EVALUATOR(x, y) LIB_PASTER(x, y)
#define LIB_FUNCTION(x)     LIB_EVALUATOR(x, LIB_VERSION_SUFFIX)

#define lib_functionA LIB_FUNCTION(lib_functionA)

extern int lib_functionA(const char *, int);

Etc.

The user of the code writes in terms of the undecorated function names (lib_functionA), while the header ensures that the correct version suffix is appended.

You do not necessarily have to modify every function; you do need to ensure that some function that will always be used is adorned with the version number. If there is an initialization function (lib_init(), perhaps), then use that. You could do it to a variable; the hard part then is ensuring that the variable is referenced in each program.

Note that it is more usual to ensure that the interface to a library remains unchanged across versions so that programs can be linked with any version without needing compilation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜