30

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)

hpique
  • 119,096
  • 131
  • 338
  • 476

2 Answers2

34

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

justin
  • 104,054
  • 14
  • 179
  • 226
  • 1
    This answer would be perfect if only it had a quote from respective link so that the reader wouldn't have to click away from SO to find the answer, unless he wanted to read more =) – jake_hetfield Aug 16 '12 at 11:06
  • @jake_hetfield thanks. unfortunately, `static` and `inline` would each require more than one page if a full quote were added to the answer. so… i don't think i will quote them =\ – justin Aug 16 '12 at 11:08
  • The links you refer to have rather short descriptions, they would fit into an answer, just my opinion. I would write a new answer but that kind of would feel like steeling yours =) – jake_hetfield Aug 16 '12 at 11:10
  • 2
    @jake_hetfield that doesn't sound very DRY – Paul.s Aug 16 '12 at 11:13
  • @jake_hetfield added some summaries – justin Aug 16 '12 at 11:25
  • 3
    @Justin I'm very happy, awesome answer! – jake_hetfield Aug 16 '12 at 11:28
  • 1
    @Paul.s I agree in keeping things DRY inside of SO, but when it comes to referring to the outside I don't think it counts. In the end I just wanted to give Justin my feedback on how to make his good answer awesome =) – jake_hetfield Aug 16 '12 at 11:29
5

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?


Community
  • 1
  • 1
jake_hetfield
  • 3,388
  • 1
  • 23
  • 30
  • `static` has different meaning in different (function/variable/local variable) contexts. – zrslv Feb 08 '14 at 08:34
  • Not really, the definition above applies to all context. Local variables that are declared static, remain alive outside of the scope. However, they are not visible outside of scope, as stated above: "The Variable is visible within the scope it was declared." In objective c static functions are declared with (+) and do not use the static keyword, thus we are only talking variables here. But the concept is the same: The static function remains the same context throughout the application. Only its visibility depends on the scope where it is declared. – jake_hetfield Feb 12 '14 at 08:58
  • Static functions are not declared with the plus sign (+). These are class level methods you're thinking of. Static functions in Objective-C are exactly what they are in C, and declared the same way. – nhgrif Aug 06 '14 at 02:35