Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ANSI-C not know the inline keyword?

Tags:

c

inline

c89

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?

like image 413
Nico Schlömer Avatar asked Aug 28 '12 00:08

Nico Schlömer


4 Answers

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.

like image 158
Reed Copsey Avatar answered Oct 24 '22 14:10

Reed Copsey


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).

like image 42
nneonneo Avatar answered Oct 24 '22 16:10

nneonneo


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

like image 22
Seçkin Savaşçı Avatar answered Oct 24 '22 14:10

Seçkin Savaşçı


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).

like image 45
teppic Avatar answered Oct 24 '22 16:10

teppic