简体   繁体   中英

Does ANSI-C not know the inline keyword?

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?

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM