Find the answer to your Linux question:
Results 1 to 5 of 5
Hello, I have a compile error introduced by the "-ansi" option of GCC which I don't understand. I can reproduce the error with some very simple code: Code: #include <netdb.h> ...
  1. #1
    Just Joined!
    Join Date
    Jun 2009
    Posts
    20

    Error when using GCC with "-ansi" option

    Hello,

    I have a compile error introduced by the "-ansi" option of GCC which I don't understand. I can reproduce the error with some very simple code:

    Code:
    #include <netdb.h>
    
    int main(int argc, char * argv[])
    {
        struct addrinfo a;
    }
    If I use the following command line:

    Code:
    gcc -ansi dummy.c -o obj/dummy.o
    Then I get an error:

    Code:
    dummy.c:5: error: storage size of ‘a’ isn’t known
    Omit the "-ansi" option and the file compiles without error.

    Here's another example in an attempt to fix the problem:

    Code:
    #include <netdb.h>
    
    typedef struct addrinfo addrinfo_t;
    
    int main(int argc, char * argv[])
    {
        addrinfo_t a;
    }
    This has the same error if the "-ansi" option is used.

    N.B. The structure "addrinfo" is defined in /usr/include/netdb.h and is well documented around the Internet.

    Any ideas?

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    The structure addrinfo is only defined in /usr/include/netdb.h if you are building for POSIX compliance. If you use the -ansi argument to gcc, you are telling it to use strict ansi compliance, which will undefine the posix macros required to define this structure. FWIW, the basic gcc is ansi-compliant. Strict ansi compliance will restrict greatly what you are able to do, as non-ansi constructs will be unavailable.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Jun 2009
    Posts
    20
    Thanks for the answer - I now understand.

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    This is a great link that will explain what's going on here...Check the options index for ansi

    GCC online documentation - GNU Project - Free Software Foundation (FSF)
    Make mine Arch Linux

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by gerard4143 View Post
    This is a great link that will explain what's going on here...Check the options index for ansi

    GCC online documentation - GNU Project - Free Software Foundation (FSF)
    Excellent link, gerard4143. One worth bookmarking! Anyway, here is what the gcc 4.4.0 documentation says about -ansi:
    -ansi
    In C mode, this is equivalent to `-std=c89'. In C++ mode, it is equivalent to `-std=c++98'.

    This turns off certain features of GCC that are incompatible with ISO C90 (when compiling C code), or of standard C++ (when compiling C++ code), such as the asm and typeof keywords, and predefined macros such as unix and vax that identify the type of system you are using. It also enables the undesirable and rarely used ISO trigraph feature. For the C compiler, it disables recognition of C++ style `//' comments as well as the inline keyword.

    The alternate keywords __asm__, __extension__, __inline__ and __typeof__ continue to work despite -ansi. You would not want to use them in an ISO C program, of course, but it is useful to put them in header files that might be included in compilations done with -ansi. Alternate predefined macros such as __unix__ and __vax__ are also available, with or without -ansi.

    The -ansi option does not cause non-ISO programs to be rejected gratuitously. For that, -pedantic is required in addition to -ansi. See Warning Options.

    The macro __STRICT_ANSI__ is predefined when the -ansi option is used. Some header files may notice this macro and refrain from declaring certain functions or defining certain macros that the ISO standard doesn't call for; this is to avoid interfering with any programs that might use these names for other things.

    Functions that would normally be built in but do not have semantics defined by ISO C (such as alloca and ffs) are not built-in functions when -ansi is used. See Other built-in functions provided by GCC, for details of the functions affected.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...