警告与nftw

LB4*_*B40 9 c unix linux posix

我正在尝试使用nftw来处理目录下的一些文件

#include <ftw.h>
#include <stdio.h>

 int wrapper(const char * fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
  printf("File %d\n", ftwbuf->base);
  return(0);
} 


int main(int argc, char ** argv) {
    const char *name;
    int flags = 0;
    name = argv[1];
    nftw(name, wrapper, 20, flags);
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

当我正在编译时(gcc kconfig_parser.c -o parser),我收到了这个警告并且出现了这个错误..

kconfig_parser.c:5: warning: ‘struct FTW’ declared inside parameter list 
kconfig_parser.c:5: warning: its scope is only this definition or declaration, which is probably not what you want
kconfig_parser.c: In function ‘wrapper’:
kconfig_parser.c:6: error: dereferencing pointer to incomplete type
Run Code Online (Sandbox Code Playgroud)

我已经检查了struct的定义和回调的原型,以及一些例子,它应该没问题......我做错了什么?(我删除了几乎所有代码以清除它)...

谢谢

Jul*_*ano 9

由于某些原因,Linux仍然在此API中使用SUSv1,其中nfsw()仍被视为扩展.

在Linux 手册页中,include必须是:

#define _XOPEN_SOURCE 500
#include <ftw.h>
Run Code Online (Sandbox Code Playgroud)

  • 只需注意:#define需要在包含第一个标题之前,甚至是不相关的标题. (5认同)