Menu

Non-working standard C functions in C99 mode?

2002-12-07
2012-09-26
  • Nobody/Anonymous

    I've been working on an application under Linux using gcc 3.0.2.  It uses a number of standard C functions, among which are the stat() and fstat() functions as defined in <sys/stat.h>.  A friend working under Windows volunteered to help me debug my code and work on future projects with me.  I suggested that he try Dev-C++ because it uses gcc and should be more or less compatible with my environment.

    After importing my project into Dev-C++ v4.9.7.0 (5.0 beta 7) with gcc 3.2, we were able to eliminate most of the compilation errors by tweaking project settings and such.  A few are lingering, and we are unable to find their causes.

    As soon as I declare a 'struct stat' to hold the return values from either stat() or fstat(), it not only complains that it can't figure out the struct's storage size, but it generates implicit declaration errors for stat(), fstat(), even fileno() which is defined in <stdio.h> and works fine when the 'struct stat' is commented out!

    The real kicker is that if I compile under mingw and leave off the '--std=c99' option to compile in C89 mode instead, I don't get these errors.  Unfortunately I've already got a rather large body of code written to the C99 spec that won't compile with this change.

    My gcc command line as shown in the Dev-C++ message window is:

    gcc testme.c -o testme -I"c:/dev-cpp/include" -std=c99

    testme.c contains:

    -------------------------8<-------------------------

    #include <sys/stat.h>
    #include <stdio.h>

    int main()
    {
        struct stat  filestat;
        FILE         *openfile;
        int          test;

        openfile = fopen("c:\\src\\main.c", "r");

        fstat(fileno(openfile), &filestat);
       
        test = filestat.st_size;

        printf("test = %d\n", test);

        fclose(openfile);

        return 0;
    }

    ---------------------8<---------------------

    And, when compiled, yields the following errors:

    testme.c: In function `main':
    testme.c:6: storage size of `filestat' isn't known
    testme.c:12: warning: implicit declaration of function `fstat'
    testme.c:12: warning: implicit declaration of function `fileno'

    I know it's generally bad to assume that just because a program will build under one compiler, it should build under another, but in this case it's the same compiler!  This works fine in gcc 3.0.2 under Linux (kernel 2.4.19, glibc 2.1, not that it matters), in C89 or C99 mode.  This problem crops up on my friend's Windows XP box and my Linux box running the mingw build tools under WINE (from the command line -- Dev-C++ doesn't work well under WINE).

    We've tried everything we can think of.  We started with gcc 2.95.3 in the other beta package (which seems to compile this example), but that had other problems.  Screenfuls of errors on my app.  I tried adding the include/ and lib/ directories to my path.  I'd download an older package, but we're both on dial-up and it takes an hour and a half or so to grab each release.

    Has anyone ever seen behavior like this?  Does the test program compile properly?  I'd hate to have him resort to MSVC.

    Thanks.

    Lime

     
    • Nobody/Anonymous

      Ack, sorry guys..  Didn't mean to post that twice.

      Lime

       
    • Nobody/Anonymous

      I had someone compile that test under gcc 3.2.1 for Linux, and it exhibited the same behavior in C99 mode.

      Lime

       
    • Nobody/Anonymous

      fstat and fileno are not standard c functions and stat.h is not standard c header, not provided by c99 or c89 standards.

      tkorrovi

       
      • Nobody/Anonymous

        You're right.  According to my man page, stat() and fstat() only conform to SVr4, SVID, X/OPEN, BSD 4.3, and POSIX.  The POSIX standard also defines fileno().

        Nevertheless, we're using gcc.  This compiler supports all three functions.  _stat(), _fstat(), stat() and fstat() are prototyped in <sys/stat.h> as bundled with mingw gcc and shipped with Dev-C++.  Likewise for fileno() in <stdio.h> as shipped with Dev-C++.

        In my experience, this support can be demonstrated by compiling the sample code in:

        gcc 2.95.3 (Windows, unable to test Linux)
        gcc 3.0.2 (C89 or C99 modes in Linux, unable to test Windows)
        gcc 3.2 (C89 mode, Windows or Linux) 

        The fileno() function also compiles properly in gcc 3.2 (C89 *and* C99 modes, Windows and Linux) when all <sys/stat.h> functions and data types are removed from the sample code.

        Using <sys/stat.h> functions and data types in gcc 3.2 compiling in C99 mode yields the errors shown above.

        Lime

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.