Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static, extern and inline in Objective-C

What do static, extern and inline (and their combinations) mean in Objetive-C using the LLVM compiler?

Also, I noticed that there are CG_EXTERN and CG_INLINE macros. Should we be using those instead?

(I couldn't find a source with a clear explanation so I thought it might be useful to create one here, or point to it if someone knows one)

like image 279
hpique Avatar asked Aug 16 '12 10:08

hpique


People also ask

What is inline in Objective C?

By declaring a function inline you tell the compiler to replace the complete code of that function directly into the place from where it was called.

What is static inline in C?

Static inline functions are simple. Either a function defined with the inline function specifier is inlined at a reference, or a call is made to the actual function. The compiler can choose which to do at each reference. The compiler decides if it is profitable to inline at -xO3 and above.

Can we extern inline function in C?

c file sees an extern declaration (without inline ) and an inline definition. Thus it emits the symbol for the function in the object file. All other compilation units only see an extern declaration, and so they can use the function without problems, if you link your final executable with the other .o file.


2 Answers

What do static, extern and inline (and their combinations) mean in Objetive-C using the LLVM compiler?

The same as in C, unless you compile as ObjC++ -- then they mean the same as found in C++.

So here is an introduction for C, but read the links if you are ready to use these because the details are important:


Extern

Summary: Indicates that an identifier is defined elsewhere.

Details: http://tigcc.ticalc.org/doc/keywords.html#extern

Static

Summary (value): Preserves variable value to survive after its scope ends.

Summary (function): Effectively emits unnamed copies - useful for private functions in C, and can be used to escape multiple definition errors when used with inline functions.

Details: http://tigcc.ticalc.org/doc/keywords.html#static

Inline

Summary: Suggests the body of a function should be moved into the callers.

Details: http://tigcc.ticalc.org/doc/gnuexts.html#SEC93


Note that inline and static are quite a bit more complex in C++ (like pretty much everything in C++).

I also found that there are CG_EXTERN and CG_INLINE macros. Should we be using those instead?

No.

Instead, you should specify your own, with your own meanings, if you need this type of functionality. CG_EXTERN and CG_INLINE have specific meanings (which may change), and are meant to be used in their defined context -- also, you don't want to have to include a whole handful of frameworks (all CoreGraphics/ApplicationServices/CoreFoundation/etc.) when you want to specify something is extern in a way that works in C and C++.

like image 55
justin Avatar answered Sep 20 '22 18:09

justin


Justin covered most of it, but I found some other nice resources for those who want to dig deeper:

By declaring a function inline you tell the compiler to replace the complete code of that function directly into the place from where it was called. This is a rather advanced feature that requires understanding of lower-level programming.

Inline functions


This SO question has an enormous answer about extern variables - variables defined "somewhere else" - but need to be used also "here".


Static preserves variable life outside of scope. The Variable is visible within the scope it was declared.

What does a static variable mean?


like image 45
jake_hetfield Avatar answered Sep 21 '22 18:09

jake_hetfield