如何修复 GCC 错误 'implicit declaration of function printf'
问题:
你有类似这样的 C 代码
main.c
printf("test");但当你尝试编译它时,你看到类似这样的警告
compile_warning.txt
main.c: In function ‘main’:
main.c:2:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
printf("test");
^~~~~~
main.c:2:5: warning: incompatible implicit declaration of built-in function ‘printf’
main.c:2:5: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’解决方案
添加
fix_stdio.c
#include <stdio.h>在警告发生的源文件顶部。
注意此警告消息只是警告,如果你正确使用 printf,你的程序即使没有 #include <stdio.h> 也能工作。但是,不正确使用 printf 和类似函数会导致难以调试的错误,因此我建议在任何情况下都添加 #include <stdio.h>,即使没有它也能工作。
Check out similar posts by category:
C/C++, GCC Errors
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow