接口冲突的一种解决方法
問題描述:在一個大的項目中往往會包括很多模塊,會有不同的部門或公司來負責實現某個模塊,也有可能有第三方或客戶的參與。假如他們都用到了某個開源軟件,底層模塊根據自身的需求對這個開源軟件進行了修改或裁減。上層也用到了此開源軟件,根據它自己的需求不需要做改動。如果在一個項目中,底層和上層分別同時引入兩次此開源軟件,就會發生接口沖突。并且底層想對上層隱藏對此開源軟件的使用,此時可以通過對接口重命名來解決沖突。
下面舉一個簡單實例來說明:
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,所以不會產生沖突。
總結
以上是生活随笔為你收集整理的接口冲突的一种解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 非对称加密算法之RSA介绍及OpenSS
- 下一篇: C/C++中“#”和“##”的作用和用法