2017-04-11 32 views
1

我需要递归搜索给定文件名的一个UNC路径(request),我已经成功连接到路径,并找到了搜索here的答案,但是,当我编译我的程序,我得到了以下错误:递归搜索一个目录,我不明白的编译错误

find-util.c: In function ‘char* search_utils(const char*, int, bool)’: 
find-util.c:51:16: error: ‘struct dirent’ has no member named ‘d_type’ 
     if (ent->d_type == DT_DIR) 
       ^
find-util.c:51:26: error: ‘DT_DIR’ was not declared in this scope 
     if (ent->d_type == DT_DIR) 
         ^

我在Ubuntu 16.04是使用mingw编译器交叉编译,以便能够在Windows 7上运行我缺少一个库?或者还有其他事情我没有看到?

#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 
#include <dirent.h> 
#include <string.h> 

static char *search_utils(const char *request, int depth, bool verbose) 
{ 
    DIR *dir; 
    struct dirent *ent; 

    puts("Connecting to mgtutils.."); 

    if ((dir = opendir(MG_PATH)) != NULL) 
    { 
     while ((ent = readdir(dir)) != NULL) 
     { 
      if (verbose == true) 
      { 
       printf("Searching%s for %s\n", ent->d_name, request); 
      } 
      if (ent->d_type == DT_DIR) 
      { 
       if ((strlen(MG_PATH) + strlen(ent->d_name) + 1) > PATH_MAX) 
       { 
        puts("Path to long, cannot"); 
       } 
      } 
     } 
    } 
} 

回答

0

,我发现在网上Linux手册页READDIR(3)这个可能的解释:

中的dirent结构的域只有由POSIX.1 规定是d_name和的d_ino。其他字段是非标准化的,并非所有系统上都存在 ;有关更多详细信息,请参阅下面的注释。

因此,请编辑您的问题并说明您正在使用的操作系统/平台和编译器。您可能还会看到dirent.h

我在cygwin上做了这个,其中/usr/include/dirent.h包括sys/dirent.h。在/usr/include/sys/dirent.h中,我找到了多种口味的struct dirent,是的,在这种情况下,存在一个d_type组件。 (所以你可能不在cygwin上...)

所以,如果struct dirent没有提供d_type在你的情况下,你还能用什么?如何stat() ...

+0

Linux的Ubuntu 16.04,mingw编译器交叉编译到Windows 7 – justAnotherCat

+0

嗯。 'readdir()'在Windows上不存在。 win32 API改为提供FindFirst()FindNext()。然而,mingw可能会像'readdir()'一样将它包装到Posix中。 (我不知道。)因此,我的答案的底部可能会导致你进入正确的方向... – Scheff

+0

思考两次我关心什么标题用于交叉编译。仔细阅读'gcc'的命令行调用(尤其是包含'-I'参数之后的目录)应该给出提示。我从来没有这样做过,但我知道有人在Linux/mingw之间交叉编译Windows的库。如果你被困住了,我可以问他... – Scheff