c和c++ 共用一份头文件

起因:

由于代码要满足misra c++2008的编码规范, 但是misra c++2008的要求中不允许使用裸指针的加减操作,因此在设计中考虑将裸指针偏移的操作挪到c中实现,misra c 2012 的要求中允许了裸指针的偏移。

然后在将一部分代码移至c的过程中发现了一个问题, 之前的公共库是 c++ 编写的,c代码无法去调用这些公共库代码,因此考虑将公共库改写为c代码,但又因为c++也要使用这部分公共库,所以唯一的办法就是c++和c共用同一份头文件(也可以是两个头文件,但这样显得代码很冗余)

因此简单记录一下一个demo!代码中屏蔽了宏定义使用的warning

// crc.h


#ifndef UTILS_CRC32_UTILS_H
#define UTILS_CRC32_UTILS_H

/* polyspace +2 AUTOSAR-CPP14:A16-0-1 [Justified:Unset] "Comment xxxx" */
/* polyspace +1 MISRA-CPP:16-2-1 [Justified:Unset] "Comment xxxx" */
#ifdef __cplusplus
#include <cstddef>
#include <cstdint>
namespace xxxxx {
extern "C" {

static const uint32_t kCrc32TableSize = 256;

/* polyspace +1 AUTOSAR-CPP14:A16-0-1 [Justified:Unset] "Comment xxxx" */
#else  // __cplusplus

#include <stddef.h>
#include <stdint.h>

#define kCrc32TableSize 256

#endif  // __cplusplus

uint32_t crc32_start(void);

uint32_t crc32_end(uint32_t crc);

uint32_t crc32_do(const void *const in_buf, uint32_t crc, const uint64_t in_buf_len);

/* polyspace +2 AUTOSAR-CPP14:A16-0-1 [Justified:Unset] "Comment xxxx" */
/* polyspace +1 MISRA-CPP:16-2-1 [Justified:Unset] "Comment xxxx" */
#ifdef __cplusplus
}
}  // namespace xxxxx
#endif  // __cplusplus
#endif  // UTILS_CRC32_UTILS_H

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值