5

Hi was reviewing some Objective-C code and found out the following statement:

OBJC_EXTERN void CLSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2);

What does this mean? Also, what is supposed to be the syntax of this statement?

Thanks in advance.

Alex Zavatone
  • 4,106
  • 36
  • 54
pinker
  • 1,283
  • 2
  • 15
  • 32

1 Answers1

5

OBJC_EXTERN is defined in <objc/objc-api.h> as

#if !defined(OBJC_EXTERN)
#   if defined(__cplusplus)
#       define OBJC_EXTERN extern "C" 
#   else
#       define OBJC_EXTERN extern
#   endif
#endif

and therefore prevents "C++ name mangling" even if the above declaration is included from a C++ source file, as for example explained here:

For pure C code, you can just remove the OBJC_EXTERN, because the extern keyword is not needed in a function declaration.


NS_FORMAT_FUNCTION is defined as

#define NS_FORMAT_FUNCTION(F,A) __attribute__((format(__NSString__, F, A)))

and __attribute__((format(...))) is a GCC specific extension, also understood by Clang:

It allows the compiler to check the number and types of the variable argument list against the format string. For example

CLSLog(@"%s", 123);

would cause a compiler warning, because %s is the placeholder for a string, but 123 is an integer.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • And there is a syntax for the extern? In this particular case, what is the meaning of the statement? – pinker Dec 23 '13 at 16:28
  • Ah, just another C++ crap. – Tricertops Dec 23 '13 at 16:30
  • @pinker: `extern` is necessary to declare (roughly speaking) "global variables". See http://stackoverflow.com/a/1433387/1187415 for a complete and perfect explanation. It might be that ancient compilers required the extern keyword also for function prototypes. – Martin R Dec 23 '13 at 16:35
  • Thanks @MartinR. And what it does mean the specific example I posted? Is being NS_FORMAT_FUNCTION(1,2) being passed as argument to CLSLog call? – pinker Dec 23 '13 at 17:35