起因:
由于代码要满足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