最近在用eRPC(https://github.com/EmbeddedRPC/erpc)实现通信时,发现一个问题,
当有两个以上eRPC服务共用时会存在类型重定义问题,比如binary_t
会在每个eRPC服务的头文件中定义一次。
解决这个问题只能修改erpcgen的模板,还好,eRPC模板代码结构比较清晰,很快就找到生成eRPC服务接口头文件的位置(${ERPC_ROOT}/erpcgen/src/templates/c_common_header.template)
只要添加类似如下的宏定义就可以解决问题
#if !define(${typename}_DEFINE)
#define ${typename}_DEFINE
#endif /** ${typename}_DEFINE */
修改后的模板文件如下: c_common_header.template
代码语言:javascript复制{% if mlComment != ""%}
{$mlComment}
{% endif %}
{$commonHeader()}
#if !defined({$commonGuardMacro})
#define {$commonGuardMacro}
{% if usedUnionType %}
#if defined(__CC_ARM) || defined(__ARMCC_VERSION)
#pragma anon_unions
#endif
{% endif -- usedUnionType %}
{% if not commonTypesFile == "" %}
// Common types header file
#include "{$commonTypesFile}"
{% else %}
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "erpc_version.h"
{% if empty(crc16) == false %}
#include "erpc_crc16.h"
{% endif -- empty(crc16) == false %}
{% for inc in includes %}
#include "{$inc}"
{% endfor -- includes %}
{$checkVersion()}
{$>checkCrc()}
{% endif -- not commonTypesFile %}
{% if commonTypesFile == "" %}
#if !defined(ERPC_TYPE_DEFINITIONS)
#define ERPC_TYPE_DEFINITIONS
{% if not empty(enums) %}
// Enumerators data types declarations
{% for enum in enums %}
{% if enum.name != "" %}
{% endif %}
{$> enum.mlComment}
{% if enum.name %}typedef {% endif --enum.name %}enum{$addIndent(" ", enum.name)}
{
{% for enumMember in enum.members %}
{$> addIndent(" ", enumMember.mlComment)}
{$enumMember.memberDeclaration}{$enumMember.ilComment}
{% endfor -- enumsMembers %}
}{$addIndent(" ", enum.name)};{$enum.ilComment}{$loop.addNewLineIfNotLast}
{% endfor -- enums %}
{% endif -- enums %}
{% if not empty(aliases) %}
// Aliases data types declarations
{% for alias in aliases %}
#if !defined({$alias.name}_TYPEDEF)
#define {$alias.name}_TYPEDEF
{$> alias.mlComment}
{% if alias.typenameName == "" %}
typedef {$alias.unnamedType}
{
{% for mem in alias.unnamed.members %}
{$mem.memberDeclaration}
{% endfor -- alias.unnamed.members %}
} {$alias.unnamedName};
{% else -- alias.typenameName %}
typedef {$alias.typenameName};{$alias.ilComment}
{% endif -- alias.typenameName %}
#endif /** {$alias.name}_TYPEDEF */
{% endfor -- aliases %}
{% endif -- aliases %}
{% if nonExternalStructUnion %}
// Structures/unions data types declarations
{% for us in symbols %}
{% if !us.isExternal %}
#if !defined({$us.name}_DEFINED)
#define {$us.name}_DEFINED
{% if us.type == "struct" %}
{$> us.mlComment}
struct {$us.name}
{
{% for mem in us.members %}
{$> addIndent(" ", mem.mlComment)}
{$mem.memberDeclaration}{$mem.ilComment}
{$> addIndent(" ", mem.elementsCount)}
{% endfor %}
};{$us.ilComment}{$loop.addNewLineIfNotLast}
{% else -- us.type == "union" %}
{$> us.mlComment}
union {$us.name}
{
{$ addIndent(" ", unionMembersDeclaration(us))}
};{$us.ilComment}{$loop.addNewLineIfNotLast}
{% endif -- us.type == "union/struct" %}
#endif /** {$us.name}_DEFINED */
{% endif -- !us.isExternal %}
{% endfor -- symbols %}
{% endif -- nonExternalStruct || nonExternalUnion %}
{% if not empty(consts) %}
// Constant variable declarations
{% for c in consts %}
{$> c.mlComment}
extern const {$c.typeAndName};{$c.ilComment}{$loop.addNewLineIfNotLast}
{% endfor -- consts %}
{% endif -- consts %}
#endif // ERPC_TYPE_DEFINITIONS
{% endif -- commonTypesFile %}
{% if not genCommonTypesFile %}
{% for iface in group.interfaces %}
/*! @brief {$iface.name} identifiers */
enum _{$iface.name}_ids
{
k{$iface.name}_service_id = {$iface.id},
{% for fn in iface.functions %}
k{$iface.name}_{$fn.name}_id = {$fn.id},
{% endfor %}
};
{% endfor %}
#if defined(__cplusplus)
extern "C" {
#endif
{% for iface in group.interfaces if iface.isNonExternalInterface == true %}
{$> iface.mlComment}
//! @name {$iface.name}
//@{
{% for fn in iface.functions if fn.isNonExternalFunction == true %}
{$> fn.mlComment}
{$fn.prototype};{$fn.ilComment}{$loop.addNewLineIfNotLast}
{% endfor -- functions %}
//@} {$iface.ilComment}
{% endfor -- iface %}
#if defined(__cplusplus)
}
#endif
{% endif -- genCommonTypesFile %}
#endif // {$commonGuardMacro}
以上模板文件在码云仓库的位置
https://gitee.com/l0km/facelog/blob/erpc/facelog-client-cpp/dependencies/erpc/erpc/erpcgen/src/templates/c_common_header.template