15

When compiling something as simple as

inline int test() { return 3; }

int main()
{
 test();
 return 0;
}

with gcc -c test.c, everything goes fine. If the -ansi keyword added, gcc -ansi -c test.c, one gets the error message

test.c:1:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’

This is true even if the C99 standard is explicitly selected, gcc -std=c99 -ansi -c test.c.

What is the reason for this, and is there a recommended fix?

4 Answers 4

17

You need to use:

gcc -std=c99 -c test.c

The -ansi flag specifies c90:

The -ansi option is equivalent to -std=c90.

ANSI C was effectively the 1990 version of C, which didn't include the inline keyword.

0
14

Nope, ANSI C doesn't have inline.

Your second command actually overrides -std=c99 with -ansi (they both affect -std=), so you are in effect compiling using ANSI C (no C99).

6

The inline keyword is not part of the original ANSI C standard (C89) so the library does not export any inline function definitions by default. Inline functions were introduced officially in the newer C99 standard but most C89 compilers have also included inline as an extension for a long time.

quoted from Gnu website

4

The reason it works fine without the ansi option at all is because gcc defaults to '-std=gnu90', which is ANSI/C89 plus extensions (one of which, not surprisingly, is support for inline functions). If you just want ANSI C support you don't need any options, unless you want strict standard compliance (which obviously may be useful if your code is going to be compiled on other compilers).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.