Thread: Problem with Header files.

  1. #1
    Registered User
    Join Date
    May 2008

    Problem with Header files.

    I have a bit of problem in compiling my c program. When I compile my program i get the following error.

    ldirfunc.c:13: error: conflicting types for opendir
    /usr/include/dirent.h:135: error: previous declaration of opendir was here
    Program snippet:

    #include "Dirent.h"
    #include "syscall.h"
    Dir *opendir(char *dirname){
      int fd;
      struct stat stbuf;
      Dir *dp;
      if((fd=open(dirname,O_RDONLY,0)) == -1 || fstat(fd,&stbuf) == -1 ||
          ((stbuf.st_mode * S_IFMT) != S_IFDIR) ||
          (dp = (Dir *) malloc (sizeof(Dir))) != NULL)
        return NULL;
      return dp;
    What I understand from the error, that my opendir function is already declared in the dirent.h(not the one i use, Dirent.h). dirent.h must have been included somewhere, may in some of the header files that i use.
    Do we have any option while compiling to neglect these redefinition errors? I want to use my opendir function and not the predefined one.
    I am a beginner in c programming, so please excuse me for my mistakes.

    thanks to all!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    I think the simplest solution is to rename your function, e.g., use a chakra prefix and name it chakra_opendir().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > #include "Dirent.h"
    Is this your dirent.h file, or the one belonging to the system?
    If it's yours, give it a different name, because relying solely on a case-sensitive filename won't work on say a windows box come porting time.

    > #include "syscall.h"
    I thought you only needed this if you were talking directly to the OS.

    > #include<malloc.h>
    malloc is prototyped in stdlib.h

    > dp = (Dir *) malloc (sizeof(Dir))
    There's no need to cast malloc in a correct C program.
    See the FAQ for other reasons why it really is not helpful to cast malloc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2008
    the mastring in c.(lawkumar)

  5. #5
    Registered User
    Join Date
    May 2008
    Thanks to all.
    I will try the suggestions provided and see if i can succeed.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Toronto, ON
    Quote Originally Posted by lawkumar3289 View Post
    the mastring in c.(lawkumar)
    Huh? What's a 'mastring'?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual C++ 05 Express Header Files
    By comwiz in forum C++ Programming
    Replies: 14
    Last Post: 02-16-2006, 07:00 AM
  2. Header Files
    By Volair in forum C Programming
    Replies: 2
    Last Post: 12-09-2005, 10:51 AM
  3. need help with menu program and header files
    By DFC in forum C++ Programming
    Replies: 12
    Last Post: 12-07-2005, 12:09 PM
  4. Missing header files and libraries with Borland
    By quagsire in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2003, 06:19 AM
  5. Problem with cgi script - can't rename files
    By bjdea1 in forum C Programming
    Replies: 2
    Last Post: 12-12-2001, 04:09 PM