声明了静态函数但从未引用错误

问题描述

对于以下 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 中的那个函数的版本不能通过名称从其他翻译单元访问。您的编译器告诉您,它也不是从同一个翻译单元通过名称访问的,并且没有公开可以调用它的指针,因此绝对不会调用该函数。这是死代码。

通常这种问题需要警告而不是错误,但也许您的编译器特别严格,或者您启用了将警告转换为错误的选项,以迫使您解决所有诊断,否则这些诊断只会是警告.无论如何,有几种可能的解决方案,但这些是最有可能的:

  1. localFunc() 中删除 file.c 及其前向声明,或者将它们注释掉,或者通过条件编译指令禁止它们。这假设它们是故意不使用的。

  2. 插入调用 localFunc() 的实时代码。只有在翻译单元中没有调用 localFunc() 是某种错误时,这才有意义。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...