C中的C名称
发布时间:2020-12-16 07:09:25 所属栏目:百科 来源:网络整理
导读:C语言不使用像C这样的名称修改.当函数原型在不同文件中声明不同时,这会导致细微的错误.简单的例子: /* file1.c */int test(int x,int y){ return y;}/* file2.c */#include stdio.hextern int test(int x);int main(){ int n = test(2); printf("n = %dn",
C语言不使用像C这样的名称修改.当函数原型在不同文件中声明不同时,这会导致细微的错误.简单的例子:
/* file1.c */ int test(int x,int y) { return y; } /* file2.c */ #include <stdio.h> extern int test(int x); int main() { int n = test(2); printf("n = %dn",n); return 0; } 使用C编译器(在我的情况下为gcc)编译此类代码时,不会报告任何错误.切换到C编译器后,链接将失败,并显示错误“未定义引用’test(int)’”.不幸的是,在实践中这并不容易 – 有些情况下C编译器接受代码(带有可能的警告消息),但使用C编译器时编译失败. 这当然是错误的编码实践 – 所有函数原型都应该添加到.h文件中,然后将其包含在实现或使用函数的文件中.不幸的是,在我的应用程序中有很多这样的情况,并且短期内无法修复所有这些情况.切换到g也是不可选的,我很快就得到了编译错误. 一种可能的解决方案是在编译C代码时使用C名称修改.不幸的是,gcc不允许这样做 – 我没有找到命令行选项来执行此操作.你知道是否可以这样做(也许使用其他编译器?).我也想知道一些静态分析工具是否能够捕捉到这一点. 解决方法
使用
splint 捕获这些错误.
foo.c的: int test(int x); int main() { test(0); } bar.c: int test(int x,int y) { return y; } 运行夹板: $splint -weak foo.c bar.c Splint 3.1.2 --- 20 Feb 2009 bar.c:1:5: Function test redeclared with 2 args,previously declared with 1 Types are incompatible. (Use -type to inhibit warning) foo.c:4:5: Previous declaration of test Finished checking --- 1 code warning (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |