C 警告:函数“fchmod”的隐式声明
我有一个使用 fchmod 的函数 createFile:
int createFile(char *pFileName) {
int ret;
if ((ret = open(pFileName, O_RDWR | O_CREAT | O_TRUNC)) < 0)
errorAndQuit(2);
fchmod(ret, S_IRUSR | S_IWUSR);
return ret;
}
在我的文件顶部,我有以下内容:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
编译时:编译器吐出:
warning: implicit declaration of function ‘fchmod’
我包含所有正确的文件,但收到此警告。即使有警告,程序也运行良好。
I have a function, createFile that uses fchmod:
int createFile(char *pFileName) {
int ret;
if ((ret = open(pFileName, O_RDWR | O_CREAT | O_TRUNC)) < 0)
errorAndQuit(2);
fchmod(ret, S_IRUSR | S_IWUSR);
return ret;
}
At the top of my file, I have the following includes:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
When compiling: the compiler spits out:
warning: implicit declaration of function ‘fchmod’
I'm including all of the correct files, yet getting this warning. The program runs fine, even with the warning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
巧合的是,
feature_test_macros(7)
联机帮助页直接回答了您的问题:By a happy coincidence, your question is directly answered by the
feature_test_macros(7)
manpage:您没有指定您使用的编译器或平台,但在我最近安装的 Linux 中,fchmod() 是在几个 #ifdef(__USD_BSD 和 __USE_XOPEN_EXTENDED)中定义的,但由几个 #ifdef 保护。
您不应该直接设置它们,而是通过 .尝试定义 _XOPEN_SOURCE_EXTENDED 或仅定义 _GNU_SOURCE 并重新编译(请注意,这些宏启用非标准功能,并且使用它们启用的功能可能会限制代码的可移植性)。
You didn't specify what compiler or platform you're using, but on my recent Linux installation, fchmod() is defined in but guarded by a couple of #ifdefs (__USD_BSD and __USE_XOPEN_EXTENDED).
You aren't supposed to set those directly, but rather via the _FOO_SOURCE macros in . Try defining _XOPEN_SOURCE_EXTENDED or just _GNU_SOURCE and recompiling (and note that these macros enable nonstandard functionality and use of the functionality they enable may limit the portability of your code).
我在构建 uml 时遇到了这个错误。
只需在引发此错误的文件中添加此行:
我相信它会注意添加上述答案中定义的宏。
I have faced this error while building uml.
Just add this line in the file where this error is thrown:
I believe it will take care about adding the macros defined in the above answers.