简体   繁体   English

nftw 警告

[英]Warning with nftw

I'm trying to use the nftw to process some files under a directory我正在尝试使用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;

}

When I'm compiling (gcc kconfig_parser.c -o parser) , I've got this warning and this error..当我编译 (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

I've checked the definition of the struct and the prototype of the callback, and some examples, it should be fine... What am I doing wrong ?我检查了结构的定义和回调的原型,还有一些例子,应该没问题……我做错了什么? (I've removed almost everything of my code to clear it)... (我已经删除了几乎所有代码以清除它)...

thanks谢谢

Linux, for some reason, still uses SUSv1 for this API, where nftw() is still considered an extension.出于某种原因,Linux 仍然使用 SUSv1 作为这个 API,其中 nftw() 仍然被认为是一个扩展。

From the Linux manual page , the include has to be:从 Linux手册页,包含必须是:

#define _XOPEN_SOURCE 500
#include <ftw.h>

Hmm.唔。 Your code works for me.你的代码对我有用。 Check your include paths, maybe?检查你的包含路径,也许? Though this is a system header, so it should be pretty hard to miss this.虽然这是一个系统标题,所以应该很难错过它。 Or were you accidentally compiling a version that didn't have the #include <ftw.h> line?还是您不小心编译了一个没有#include <ftw.h>行的版本?

$ gcc -o ftw ftw.c
$ ./ftw my-directory
File 10
File 11
File 16
File 16
File 16
File 16
File 16
... etc ...

edit : The test above was done on Mac OS X. In a (now deleted) comment the OP mentioned he was on Debian, for which the man page mentions that #define _XOPEN_SOURCE 500 is necessary, as Juliano points out.编辑:上面的测试是在 Mac OS X 上完成的。在(现已删除的)评论中,OP 提到他在 Debian 上,为此手册页提到#define _XOPEN_SOURCE 500是必要的,正如 Juliano 指出的那样。

On a CentOs versions the header file didn't use "#define _XOPEN_SOURCE 500" i had to do this below,在 CentOs 版本上,头文件没有使用“#define _XOPEN_SOURCE 500”我必须在下面这样做,

#define __USE_XOPEN_EXTENDED 1
#include <ftw.h>

在 Ubuntu 18.04 上,这似乎是现在可行的(类似于 JohnMeg 提到的 CentOS)。

#define __USE_XOPEN_EXTENDED 1 #include <ftw.h>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM