问题描述
对于以下 C 代码:
file.h
// declaration of extern function
extern void extFunc();
file.c
#include "file.h"
// declaration of static function
static void localFunc(uint32_t const input);
// definition of static function
void localFunc(uint32_t const input)
{
// do something
}
// definition of extern function
void extFunc()
{
// do something
}
当我为 ARM ISA 目标编译上述代码时,我收到编译器错误说
错误[Pe177]:函数“localFunc”已声明但从未在 C:\Users\admin\git\file.c 中引用
如何摆脱它?
解决方法
错误[Pe177]:函数“localFunc”已声明但从未在 C:\Users\admin\git\file.c 中引用
如何摆脱它?
localFunc()
被声明为 static
。这意味着出现在 file.c
中的那个函数的版本不能通过名称从其他翻译单元访问。您的编译器告诉您,它也不是从同一个翻译单元通过名称访问的,并且没有公开可以调用它的指针,因此绝对不会调用该函数。这是死代码。
通常这种问题需要警告而不是错误,但也许您的编译器特别严格,或者您启用了将警告转换为错误的选项,以迫使您解决所有诊断,否则这些诊断只会是警告.无论如何,有几种可能的解决方案,但这些是最有可能的:
-
从
localFunc()
中删除file.c
及其前向声明,或者将它们注释掉,或者通过条件编译指令禁止它们。这假设它们是故意不使用的。 -
插入调用
localFunc()
的实时代码。只有在翻译单元中没有调用localFunc()
是某种错误时,这才有意义。