0

I'm trying to understand what really happens at compile time and runtime with imported files.

Does #import "file.h" directive essentially copy and paste the entire file.m into the current file? Or does it just specify that file's location and create the necessary attributes as they are instantiated?

rmaddy
  • 298,130
  • 40
  • 468
  • 517
user
  • 3,272
  • 7
  • 27
  • 66

2 Answers2

2

The imports are handled by the preprocessor in C, C++, and Objective C, which creates one large file for the compiler. Every *.m, *.c, *.cpp file will each get all of the imports.

You can compile code on the command line with the -E flag to see the result after all the #imports are added.

Additionally, this question goes into some detail about #include vs #import, so it might give you more insight:

What is the difference between #import and #include in Objective-C?

As you can image, having lots of extra imports slows compilation. Jetbrain's AppCode has a feature that will optimize imports:

http://www.jetbrains.com/objc/features/

Community
  • 1
  • 1
h4labs
  • 695
  • 1
  • 8
  • 21
  • 2
    Note you can also see the preprocessed output in Xcode by selecting Product > Generate Output > Preprocessed File. – CRD Apr 28 '13 at 05:01
1

Does #import file.h statement essentially copy and paste the entire file.m into the current file?

  1. It is not a statement, it is a preprocessor directive.

  2. You're missing quotes or angle brackets around the file name.

  3. #import "file.h" does indeed copy the whole file.h file in place of this directive into the current file. It doesn't, however, do anything with file.m.

  • Edited for proper grammar. Now, then how does the compiler load the `methodFromFile`? Does it look up the definition when and create it when called, or was it already in memory from the time the #import _directive_ was declared? – user Apr 28 '13 at 04:03
  • 1
    @123 I don't understand that. The compiler does nothing else than taking whichever file you feed it, and generates machine code from it. (More specifically, and object file is generated from `.m` and `.c` files which contain actual code. `.h` files, which in general contain declarations and type definitions only, don't result in executable code.) The linker then takes the object files and makes an executable out of them. But this is a later and completely different state, reached strictly after the preprocessing phase. –  Apr 28 '13 at 04:36