3

I am working on a C project where I need to get the list of files that are within a directory. I am using dirent.h but am having some problems getting it to work, I am building the program under Linux.

When I try and build the program I get the following error

myClass:error: âDIRâ undeclared (first use in this function)
myClass:408: error: (Each undeclared identifier is reported only once
myClass:408: error: for each function it appears in.)
myClass:408: error: âdirâ undeclared (first use in this function)
myClass:410: warning: implicit declaration of function âopendirâ
myClass:413: warning: implicit declaration of function âreaddirâ
myClass:413: warning: assignment makes pointer from integer without a cast
myClass:415: error: dereferencing pointer to incomplete type
myClass:417: warning: implicit declaration of function âclosedirâ

Below is the code that I am using

int logMaintenance(void *arg)
{
    DIR *dir;
    struct dirent *ent;
    dir = opendir(directory);
    if (dir != NULL)
    {
        while ((ent = readdir (dir)) != NULL)
        {
            printf("%s\n", ent->d_name);
        }
        closedir(dir);
    }
    else
    {
        printf("Failed to read directory %i", EXIT_FAILURE);
    }
    return 0;
}

I don't understand what these errors mean especially when it says that DIR is undeclared when I have included the dirent.h header file for Liunux.

Thanks for your help.

Boardy
  • 35,417
  • 104
  • 256
  • 447
  • 6
    It **really** looks like you haven't included `` in your code. How are you including it? Are you _certain_ it's included in that c file? – Mat Apr 30 '12 at 13:13
  • Definetely, its at the top of the file, I've got **#include "dirent.h"** and also VS2010 is picking up that the file is included – Boardy Apr 30 '12 at 13:17
  • 2
    Ah, VS... http://stackoverflow.com/questions/883594/microsoft-visual-studio-opendir-and-readdir-how (should be `#include `, but you'll need to use Windows APIs or another compiler AFAIK) – Mat Apr 30 '12 at 13:20
  • there is slight difference between #include and #include "dirent.h". you should use , which fetches the system-headerfile, instead of the local-your-source-related headerfile. – Peter Miehle Apr 30 '12 at 13:23
  • I've tried that but still displays the same error. I'm not actually compiling on VS2010 instead just using it to add the code and then copying the source to a linux server to perform the build – Boardy Apr 30 '12 at 13:26
  • The you're "doing something wrong". Try compiling `#include ` / `int main(void){opendir("foo");}`. – Mat Apr 30 '12 at 13:35
  • @Boardy:What compiler you're using? – Jack Apr 30 '12 at 14:57
  • I've got it working now once I'd changed to @Mat second comment about using instead of "dirent.h" it still wasn't working. But have just realised in my desperate attempt to get it working, I had downloaded and copied the header file in with the C code so it was using this instead of the linux version. Once I had removed this extra file it built fine. Thanks for your help – Boardy Apr 30 '12 at 15:51
  • @Mat do you want to add that instead of "dirent.h" as an answer and I can accept it. – Boardy Apr 30 '12 at 15:52

2 Answers2

3

You should make sure that:

  • You #include <dirent.h>, rather than "dirent.h", so that the system search path for headers is used to locate that file
  • You don't have a dirent.h file lying around somewhere in your project that could be picked up instead.

When trying to debug this type of strange problem, ask GCC for the pre-processed output with gcc -E. You can see what files (including the paths) it's including. That can help a lot.

And if you're using Microsoft Visual Studio, head over to this question:
Microsoft Visual Studio: opendir() and readdir(), how?

Community
  • 1
  • 1
Mat
  • 202,337
  • 40
  • 393
  • 406
0

I'm not sure, but it seems like I was always told that you always need a main function... However I have only a mere 8 months (2 semesters) of C++ under my belt. I just practice it to be safe, however, I would also use:

int main(int argc, char **argv) or int main(int argc, char *argv[]) rather than int logMaintenance(void *arg)

(while using dirent.h).

hologram
  • 533
  • 2
  • 5
  • 21
codiddle
  • 21
  • 1
  • 2
    This doesn't really have anything to do with the question. The code in the question is a snippet, not a complete program. – Mac Dec 02 '12 at 19:40
  • This does not seem like a definitive answer, maybe it would be more appropriate and helpful as a comment – Alex Naspo Dec 02 '12 at 19:41
  • Yes you should always have a main function but this is not related to what I was asking. I provided a code snippet of the code that I needed help with, there is no need for me to include the main function as well – Boardy Dec 03 '12 at 09:39