splint 如何知道我的函数没有在另一个文件中使用?

发布于 2024-07-17 08:49:49 字数 738 浏览 13 评论 0原文

Splint 给了我以下警告:

encrypt.c:4:8: Function exported but not used outside encrypt: flip
  A declaration is exported, but not used outside this module. Declaration can
  use static qualifier. (Use -exportlocal to inhibit warning)
   encrypt.c:10:1: Definition of flip

因为我只在这个文件上调用了 splint,它怎么知道这一点?

#include        <stdio.h>
#include        <stdlib.h>

int    flip( int a)
{
        int b;
        b = a;
        b ^= 0x000C;
        return b;
}

int     blah(int argc, char    *argv[]) {

        FILE    *fp = NULL, *fpOut=NULL;
        int             ch;
        ch = 20; flip(20); return (ERROR_SUCCESS);
}

我什至摆脱了 main,以便它无法以任何方式确定文件是否完整。 我完全被难住了!

Splint gives me the following warning:

encrypt.c:4:8: Function exported but not used outside encrypt: flip
  A declaration is exported, but not used outside this module. Declaration can
  use static qualifier. (Use -exportlocal to inhibit warning)
   encrypt.c:10:1: Definition of flip

Since I called splint only on this file how does it know that?

#include        <stdio.h>
#include        <stdlib.h>

int    flip( int a)
{
        int b;
        b = a;
        b ^= 0x000C;
        return b;
}

int     blah(int argc, char    *argv[]) {

        FILE    *fp = NULL, *fpOut=NULL;
        int             ch;
        ch = 20; flip(20); return (ERROR_SUCCESS);
}

I even got rid of main so that it could not figure out that the file is complete in any way. I am totally stumped!

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

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

发布评论

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

评论(4

枫以 2024-07-24 08:49:49

您可能会发现,如果您包含一个声明 flip() 的标头(当然,您应该这样做),那么 splint 就不会抱怨。 您还应该在标头中声明 blah()

我不完全相信这就是解释,因为根本没有使用 blah() (尽管它使用 flip())并且您没有提到 splint 抱怨这一点。

但是,最好将每个函数(在 C 中)设为静态,直到您能够证明在其源文件之外需要该函数,然后确保有一个声明该函数的标头,该标头用于定义该函数的文件以及使用该函数的每个文件中。

在 C++ 中,“每个函数都应该是静态的”建议变成“每个函数都应该在匿名命名空间中定义”。

You might find that if you included a header that declared flip() - as you should, of course - then splint would not complain. You should also declare blah() in the header as well.

I'm not wholly convinced that this is the explanation because blah() is not used at all (though it uses flip()) and you don't mention splint complaining about that.

However, it is a good practice to make every function (in C) static until you can demonstrate that it is needed outside its source file, and then you ensure that there is a header that declares the function, and that header is used in the file that defines the function and in every file that uses the function.

In C++, the 'every function should be static' advice becomes 'every function should be defined in the anonymous namespace'.

傲娇萝莉攻 2024-07-24 08:49:49

由于我只在此文件上调用 splint,它怎么知道这一点?

你已经回答了你的问题。 您已经向 lint 提供了一个文件,因此 lint 知道 只有一个文件需要处理(当然,除了标准头文件之外)。

Since I called splint only on this file how does it know that?

You have answered your question. You've fed in one file to lint, so lint knows there is only file to be taken care of (apart from the standard header includes, of course).

情话墙 2024-07-24 08:49:49

int Flip() 未声明为静态,因此可以在外部使用。 由于您仅使用一个源文件调用 splint,因此它正确地表明您的函数如果不在外部使用,则必须声明为静态

int flip() is not declared as static, so it can be potentially used externally. Since you invoked splint with only one source file, it correctly says that your function, if not used externally, must be declared static

半步萧音过轻尘 2024-07-24 08:49:49

它只能报告它所看到的内容。 如果您比警告内容更了解,请忽略警告或按照说明进行抑制。 不要认为这样的工具一定比您更了解您的程序。

如果它确实不打算在文件外部使用,您可以将其声明为静态,它应该可以纠正问题,但其他文件将无法访问它。

It can only report on what it sees. Ignore the warning or follow the instructions to inhibit it if you know better than what it says. Don't assume that a tool like this necessarily knows your program better than you do.

If it really is not intended to be used outside of the file, you can declare it static and it should correct the problem, but it will be inaccessible from other files.

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