Keil STM32 C/C++混合编程 踩坑
出现class未定义
问题: ../Inc/xxx.hpp(6): error: #20: identifier "class" is undefined
解决: 检查是否在.c文件中引用了hpp头文件. 可以尝试将.c文件改为.cpp, c风格定义的函数不需要改动, 然后在此文件的.h中, 用extern "C"包裹住原本的c函数声明. 例如:
#ifdef __cplusplus
extern "C"{
#endif
void f(void);
#ifdef __cplusplus
}
#endif
.o 文件 出现未定义的函数
问题: xxx\xx.axf: Error: L6218E: Undefined symbol Foo() (referred from Bar.o).
解决:
如果.cpp文件中用到了.c文件中定义的函数, 那么此.c的头文件.h中的函数声明必须包裹在extern "C"中.
所以是Bar.cpp中使用了Foo()函数, Foo()函数的声明就必须包在extern "C"中.