问题描述:在一个大的项目中往往会包括很多模块,会有不同的部门或公司来负责实现某个模块,也有可能有第三方或客户的参与。假如他们都用到了某个开源软件,底层模块根据自身的需求对这个开源软件进行了修改或裁减。上层也用到了此开源软件,根据它自己的需求不需要做改动。如果在一个项目中,底层和上层分别同时引入两次此开源软件,就会发生接口冲突。并且底层想对上层隐藏对此开源软件的使用,此时可以通过对接口重命名来解决冲突。
下面举一个简单实例来说明:
1、 首先模拟一个简单的开源库integerArithmetic.lib,实现简单的整数加、减、乘、除运算:包含2个文件,funset.h和funset.cpp,作为上层直接调用;
funset.h:
#ifndef _FUNSET_H_ #define _FUNSET_H_ int add(int a, int b); int sub(int a, int b); int mul(int a, int b); int division(int a, int b); #endif //_FUNSET_H_
#include "funset.h"
int add(int a, int b)
{
return (a + b);
}
int sub(int a, int b)
{
return (a - b);
}
int mul(int a, int b)
{
return (a * b);
}
int division(int a, int b)
{
if (b == 0) return -1;
return (a / b);
}2、 对原integerArithmetic库进行改写,重新生成fbc.lib:包含5个文件,fbc.h, funset.h, rename.h, root.h和funset.cpp,fbc.h为上层提供的接口;
funset.h:
#ifndef _FUNSET_H_ #define _FUNSET_H_ int add(int a, int b); int sub(int a, int b); int mul(int a, int b); int division(int a, int b); #endif //_FUNSET_H_
#include "root.h"
int add(int a, int b)
{
return (a + b) / 2;
}
int sub(int a, int b)
{
return (a - b) / 2;
}
int mul(int a, int b)
{
return (a * b) / 2;
}
int division(int a, int b)
{
if (b == 0) return -1;
return (a / b) / 2;
}#ifndef _DONT_RENAME_ //To avoid any possible linking confliction, rename all exported names #define add RENAME_add #define sub RENAME_sub #define mul RENAME_mul #define division RENAME_division #endif //!_DONT_RENAME_
#ifndef _ROOT_H_ #define _ROOT_H_ #include "rename.h" #include "funset.h" #endif //_ROOT_H_
#include "root.h" #define fbc_add add #define fbc_sub sub #define fbc_mul mul #define fbc_division division
3、 新建一个testRename控制台工程,测试这两个库,包含6个文件:testfbc.h, testfbc.cpp, testintegerArithmetic.h,testintegerArithmetic.cpp, stdafx.cpp, testRename.cpp:
testfbc.h:
#ifndef _TEST_FBC_H_ #define _TEST_FBC_H_ void cal_fbc(int a, int b); #endif //_TEST_FBC_H_
#include "stdafx.h"
#include "testfbc.h"
#include "../fbc/fbc.h"
#include <iostream>
using namespace std;
void cal_fbc(int a, int b)
{
cout<<"test fbc lib:"<<endl;
cout<<"add = "<<fbc_add(a, b)<<endl;
cout<<"sub = "<<fbc_sub(a, b)<<endl;
cout<<"mul = "<<fbc_mul(a, b)<<endl;
cout<<"division = "<<fbc_division(a, b)<<endl;
}#ifndef _TEST_INTEGER_ARITHMETIC_H_ #define _TEST_INTEGER_ARITHMETIC_H_ void cal_integerArithmetic(int a, int b); #endif//_TEST_INTEGER_ARITHMETIC_H_
#include "stdafx.h"
#include "testintegerArithmetic.h"
#include "../integerArithmetic/funset.h"
#include <iostream>
using namespace std;
void cal_integerArithmetic(int a, int b)
{
cout<<"test integerArithmetic lib:"<<endl;
cout<<"add = "<<add(a, b)<<endl;
cout<<"sub = "<<sub(a, b)<<endl;
cout<<"mul = "<<mul(a, b)<<endl;
cout<<"division = "<<division(a, b)<<endl;
}#include "stdafx.h" // TODO: reference any additional headers you need in STDAFX.H // and not in this file #pragma comment(lib, "../debug/fbc.lib") #pragma comment(lib, "../debug/integerArithmetic.lib")
#include "stdafx.h"
#include <iostream>
#include "testintegerArithmetic.h"
#include "testfbc.h"
using namespace std;
int main(int argc, char* argv[])
{
int a, b;
a = 10;
b = 2;
cal_integerArithmetic(a, b);
cal_fbc(a, b);
cout<<"ok"<<endl;
return 0;
}
测试运行,结果正确,这两个库中包含了重名的add, sub, mul, division四个函数,在testRename中同时调用这两个库并没有产生冲突。
使用dumpbin工具查看fbclib.lib,Symbol Name分别为RENAME_add, RENAME_sub, RENAME_mul, RENAME_division;而integerArithmetic.lib中Symbol Name分别为add, sub, mul, division,所以不会产生冲突。
原文:http://blog.csdn.net/fengbingchun/article/details/43968485