splint 如何知道我的函数没有在另一个文件中使用?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能会发现,如果您包含一个声明
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 - thensplint
would not complain. You should also declareblah()
in the header as well.I'm not wholly convinced that this is the explanation because
blah()
is not used at all (though it usesflip()
) and you don't mentionsplint
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'.你已经回答了你的问题。 您已经向 lint 提供了一个文件,因此 lint 知道 只有一个文件需要处理(当然,除了标准头文件之外)。
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).
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
它只能报告它所看到的内容。 如果您比警告内容更了解,请忽略警告或按照说明进行抑制。 不要认为这样的工具一定比您更了解您的程序。
如果它确实不打算在文件外部使用,您可以将其声明为静态,它应该可以纠正问题,但其他文件将无法访问它。
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.