2

Possible Duplicate:
How to include header file through makefile

I am using gcc for compiling my code. I have a few make files also. Can I tell the compiler to include a particular header file to be automatically included in the all CPP files.

How can I do this?

Community
  • 1
  • 1
NeonGlow
  • 1,659
  • 4
  • 18
  • 36
  • 3
    It makes oh so much more sense to put those into a header called `feature_support.h` or something and include that in every file that needs it. – chris Nov 30 '12 at 06:08
  • @chris : I have seen this process automated in large code bases. But cant rememeber how it was done... :) – NeonGlow Nov 30 '12 at 06:19
  • If you need it in every file, there may be a problem with your design. – Peter Wood Nov 30 '12 at 08:49
  • @peter : I don't need it in every file. But it will be great if it has visibility in every file... – NeonGlow Nov 30 '12 at 09:29
  • 1
    @NeonGlow See [#ifdef considered harmful](http://www.ethernut.de/pdf/ifdefs.pdf). That said, they do suggest if you use `#ifdef` you should do it by feature rather than, for example, system architecture. They explore other, better ways to organise code. – Peter Wood Nov 30 '12 at 10:59
  • @Wood : I think most of the points in the shared link are applicable for #if, not #ifdef. – NeonGlow Dec 03 '12 at 03:50

2 Answers2

10

Add the appropriate preprocessor option for gcc to your Makefile-s and have a common_feature_header.h header file defining these.

 # in Makefile
 CPPFLAGS+= -include common_feature_header.h

If you just want to define some preprocessor flags, you don't need a common_feature_header.h file, but simply add into your Makefile the following definition

 CPPFLAGS+= -DSOME_FEATURE_FLAG=1 -DSOME_OTHERFEATURE_FLAG=0 \
            -DYET_ANOTHER_THING=2

See also this answer.

P.S. You may want to use remake (notably with its -x option) to debug your Makefile.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thanks... I'm new to make files. So I will appreciate a little more help understanding this. From what you said, I understand that I need to define each variable from the header file in make file also, say if I have 10 #defines in common_feature_header.h I need corresponding 10 entries in make file. Is that correct? [Sorry for asking a probably stupid question] – NeonGlow Nov 30 '12 at 06:50
  • No, you can have several `-Dxxx` options, on the same `CPPFLAGS=`... setting in your `Makefile` – Basile Starynkevitch Nov 30 '12 at 06:52
1

In the makefile itself directly, you can do a -DFEATURE1_SUPPORTED=1 for it to take effect on all CPP's using it.

Refer to the SO question: Is it possible to define a C macro in a makefile?

Community
  • 1
  • 1
Jay
  • 24,173
  • 25
  • 93
  • 141