[英]Warning with nftw
我正在嘗試使用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
我檢查了結構的定義和回調的原型,還有一些例子,應該沒問題……我做錯了什么? (我已經刪除了幾乎所有代碼以清除它)...
謝謝
出於某種原因,Linux 仍然使用 SUSv1 作為這個 API,其中 nftw() 仍然被認為是一個擴展。
從 Linux手冊頁,包含必須是:
#define _XOPEN_SOURCE 500
#include <ftw.h>
唔。 你的代碼對我有用。 檢查你的包含路徑,也許? 雖然這是一個系統標題,所以應該很難錯過它。 還是您不小心編譯了一個沒有#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 上,為此手冊頁提到#define _XOPEN_SOURCE 500
是必要的,正如 Juliano 指出的那樣。
在 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.