项目软件集成三方模块,编译中int32和uint32定义冲突解决方法

 软件A中需要集成某音频引擎BB引擎库提供两个头文件:B_commontype.hB_API.hB_commontype.h中定义引擎用到的基本数据类型,B_API.hinclude B_commontype.h文件并定义接口函数,接口函数使用B_commontype.h定义的基本数据类型。

在需要使用该引擎库函数的Apply_B.cpp文件中将B_commontype.hB_API.h include进去,

#include "StdAfx.h"

#include "Apply_B.h"

#include "B_commontype.h"

#include "B_API.h"

然后调用引擎中提供的函数。

 vs编译时报错:

B_commontype.h(273) : error C2371: 'int32' : redefinition; different basic types

B_commontype.h(276) : error C2371: 'uint32' : redefinition; different basic types

Apply_B.cpp(801) : error C2664: 'B_GetCodecs' : cannot convert parameter 1 from 'int *' to 'int32 *'

……

 

分析后了解:

该音频引擎库在B_commontype.h定义了:

typedef signed int        int32_t;

typedef unsigned int      uint32_t;

 

typedef int32_t             int32;

typedef uint32_t            uint32;

 

项目软件A在自己的类型定义头文件A_commonDef.h定义了

#if !defined(int32)

typedef long int32;

#endif

 

#if !defined(uint32)

typedef unsigned long uint32;

#endif

Apply_B.cpp编译时候先将A_commonDef.h包含进去,将int32定义为long类型,接着再将B_commontype.h包含进去编译,而B_commontype.hint32定义为int,所以出现上面描述的编译错误。

 

可选解决方案:

刚开始考虑将B音频引擎用一个类封装起来,这样B_commontype.h定义的类型和B_API.h定义的接口函数都不公开了,由该封装类的接口函数代替,如此可避免BAint32 uint32定义冲突问题,但是B模块接口函数太多,这个类实都要一一实现这些接口函数,不堪重负。

后来想到下面这个办法解决,十行代码就修改好了,美中不足的是需要修改B模块的接口头文件。

具体实现:

B_commontype.h,如下注释掉int32uint32typedef

//typedef int32_t             int32;

//typedef uint32_t            uint32;

这样工程在编译B_commontype.h时候,由于int32没有被定义为int32_t类型,于是编译器将int32编译成A_commonDef.h中定义的long类型和unsigned long类型,

随后编译到B_API.h,在B_API.h中的#include "B_commontype.h"语句后紧接着加上:

#define int32 int32_t

#define uint32 uint32_t

这样在编译B_commontype.h中的接口函数时候,接口函数用到的int32uint3都被替换成int32_tuint32_t,此时的int32_tint32_t都是B_commontype.h定义下的类型,

 

B_API.h文件最后加上:

#undef int32

#undef uint32

来取消前面#define int32 int32_t #define uint32 uint32_t的效果

这样,我们在使用B引擎时候可以保留了接口函数原来时候的类型,而且在A其他地方继续使用A_commonDef.h定义的类型,A,B不会再在编译时候产生类型冲突报错。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章