1

It is challenging to port from Linux to OS/X. I have an inline function embedded inside another function body. On Linux, gcc happily compiled the code, but on OS/X, clang reports error.

Here is the code snippet,

$ cat inline.c void func() { inline int max(int a, int b) { return (a>b) ? a : b; } int c = max(11,22); } 

On Linux, everything is fine,

Linux $ gcc -c inline.c Linux $$ gcc --version gcc (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010 

However, clang on OS/X complains,

OSX $ cc -c inline.c inline.c:2:38: error: function definition is not allowed here inline int max(int a, int b) { return (a>b) ? a : b; } ^ inline.c:3:17: warning: implicit declaration of function 'max' is invalid in C99 [-Wimplicit-function-declaration] int c = max(11,22); ^ 1 warning and 1 error generated. OSX $ cc --version Apple LLVM version 7.3.0 (clang-703.0.31) Target: x86_64-apple-darwin15.5.0 

Is this a gcc "feature" or there is a clang flag to enable this capability?

3
  • How many times do you call max in that function in the real code? Commented Aug 5, 2016 at 6:02
  • Yes, it is a GCC feature. Nested functions are not standard C. Clang is within its right to object. If you want your code to be portable, don't do it. Commented Aug 5, 2016 at 6:07
  • And inline here makes no sense at all. It is a precious tool if you want to place function definitions in header files such they are visible in several .c files. Everything that is found inside the same .c file can be inlined by the compiler without notice, here it is simply superfluous. Commented Aug 5, 2016 at 6:44

2 Answers 2

2

GCC extensions not implemented yet

clang does not support nested functions; this is a complex feature which is infrequently used, so it is unlikely to be implemented anytime soon.

Just move the max out the function and make it static.

Sign up to request clarification or add additional context in comments.

Comments

0

Here, "inline" means "inline substitution", rather than "inline definition".

According to N1570:

6 A function declared with an inline function specifier is an inline function. Making a function an inline function suggests that calls to the function be as fast as possible.138) The extent to which such suggestions are effective is implementation-defined.139)

138) By using, for example, an alternative to the usual function call mechanism, such as ''inline substitution''. ...

And you shouldn't define another function in a function definition. Also, use static inline instead of inline would make your life easier.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.