解决有关“未使用”功能的夹板警告 当它们作为参数传递时

发布于 2024-07-11 18:18:53 字数 867 浏览 6 评论 0 原文

在我的程序中,splint 检查器警告:

expat-test.c:23:1: Function exported but not used outside expat-test: start
  A declaration is exported, but not used outside this module. Declaration can
  use static qualifier. (Use -exportlocal to inhibit warning)
   expat-test.c:38:1: Definition of start

start() 函数 使用过。 该程序使用 expat XML 解析器来处理回调。 你给解析器一个函数:

XML_SetElementHandler(parser, start, end);

解析器在某些时候回调它。 这是 C 语言中非常常见的习惯用法,我想知道为什么 splint 会抱怨。 我在 常见问题解答手册

On a program of me, the splint checker warns:

expat-test.c:23:1: Function exported but not used outside expat-test: start
  A declaration is exported, but not used outside this module. Declaration can
  use static qualifier. (Use -exportlocal to inhibit warning)
   expat-test.c:38:1: Definition of start

The start() function is used. The program uses the expat XML parser which works with callbacks. You give the parser a function:

XML_SetElementHandler(parser, start, end);

and the parser calls it back at some points. This is a very common idiom in C and I wonder why splint complains. I find nothing in the FAQ or in the manual.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花落人离去 2024-07-18 18:18:53

您是否在定义 start() 的同一翻译单元(通常是 .c 源文件)中调用 XML_SetElementHandler() ? 如果是这样,则警告可能是正确的:将 static 添加到函数定义中,并检查您的应用程序是否仍然链接而没有错误。

Do you call XML_SetElementHandler() in the same translation unit (normally the .c source file) in which start() is defined? If so, the warning might be correct: Add static to the function definition and check if your application still links without errors.

猫烠⑼条掵仅有一顆心 2024-07-18 18:18:53

“static”关键字有效地对其他翻译单元(通常是.C 文件)隐藏了函数的名称。 代码仍然在那里,从该 C 文件中您可以获取该函数的地址(但不能从其他 C 文件中获取)。 然后,您可以通过调用函数、从函数返回地址、或者将其存储在全局变量中等方式将地址传递给其他翻译单元。

The 'static' keyword effectively hides the name of a function from other translation units (.C file, usually). The code's still there, and from that C file you can get the address of the function (but not from other C files). You can then pass the address to other translation units, either by calling functions, or returning the address from a function, or by storing it in a global variable, etc.

最美不过初阳 2024-07-18 18:18:53

我倾向于声明所有未导出为静态的函数。 我接受过教育,目前相信这样做是很好的做法。 (免责声明:与大多数事情一样,这条“规则”也有很多例外。)

I tend to declare all functions which are not being exported as static. I have been taught and currently believe that it is good practice to do so. (Disclaimer: As with most things, there are numerous exceptions to this 'rule'.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文