接口冲突的一种解决方法

问题描述:在一个大的项目中往往会包括很多模块,会有不同的部门或公司来负责实现某个模块,也有可能有第三方或客户的参与。假如他们都用到了某个开源软件,底层模块根据自身的需求对这个开源软件进行了修改或裁减。上层也用到了此开源软件,根据它自己的需求不需要做改动。如果在一个项目中,底层和上层分别同时引入两次此开源软件,就会发生接口冲突。并且底层想对上层隐藏对此开源软件的使用,此时可以通过对接口重命名来解决冲突。

下面举一个简单实例来说明:

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_

funset.cpp:

#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_

funset.cpp:

#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;
}

rename.h:

#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_

root.h:

#ifndef _ROOT_H_
#define _ROOT_H_

#include "rename.h"
#include "funset.h"

#endif //_ROOT_H_

fbc.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_

testfbc.cpp:

#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;
}

testintegerArithmetic.h:

#ifndef _TEST_INTEGER_ARITHMETIC_H_
#define _TEST_INTEGER_ARITHMETIC_H_

void cal_integerArithmetic(int a, int b);

#endif//_TEST_INTEGER_ARITHMETIC_H_

testintegerArithmetic.cpp:

#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;
}

stdafx.cpp:

#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")

testRename.cpp:

#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,所以不会产生冲突。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值