Rahul Iyer
Rahul Iyer

Reputation: 21025

Should Header files be added to the target?

When you create a .h file, Xcode unchecks the "targets" box. But normally adds .cpp / .m / .mm files.

When should and should not a .h file be treated as a target in a build system?

Upvotes: 2

Views: 1523

Answers (2)

Adi Shavit
Adi Shavit

Reputation: 17275

Source files like .cpp / .m and .mm files get compiled into binaries (usually .obj files).

Header files, e.g. .h, get #includeed into the source files by the C-pre-processor (almost verbatim at the text level), and get integrated into the source file's build, so there is (generally) no need to independently compile a header file.

Upvotes: 3

Mats Petersson
Mats Petersson

Reputation: 129374

Because .h files are included in your source files. A "target" is something that you have to compile on its own (a "source file").

Say we have

foo.cpp

  #include "foo.h"

  ... 
  ...  lots more stuff here ... 
  ...

  int main()
  {
     ... some code goes here ... 
  }

then the compiler will compile foo.cpp into an object file, and using the linker component making it into an executable file.

The foo.h file is included in your foo.cpp, and gets compiled there. If we have a complex project, bar.cpp may also include foo.h, so it gets compiled in twice - this is not a problem [typically], since header files should only contain things that can be compiled into your executable many times - if that isn't the case, "you're doing it wrong".

Upvotes: 3

Related Questions