12

我正在尝试使用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;

}

当我编译 (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

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

谢谢

4

4 回答 4

12

出于某种原因,Linux 仍然为此 API 使用 SUSv1,其中 nftw() 仍被视为扩展。

在 Linux手册页中,包含必须是:

#define _XOPEN_SOURCE 500
#include <ftw.h>
于 2009-04-23T16:00:07.127 回答
1

唔。你的代码对我有用。检查您的包含路径,也许?虽然这是一个系统头文件,所以应该很难错过它。还是您不小心编译了没有该#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 ...

编辑:上面的测试是在 Mac OS X 上完成的。在(现已删除)评论中,OP 提到他在 Debian 上,正如 Juliano 指出的那样,手册页提到这是必要的。#define _XOPEN_SOURCE 500

于 2009-04-23T15:56:21.193 回答
0

在 CentOs 版本上,头文件没有使用“#define _XOPEN_SOURCE 500”,我必须在下面这样做,

#define __USE_XOPEN_EXTENDED 1
#include <ftw.h>
于 2020-01-24T03:00:04.673 回答
0

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

#define __USE_XOPEN_EXTENDED 1 #include <ftw.h>

于 2020-02-07T21:56:32.910 回答