Including header files in multiple c files automatically?

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've got a header file that defines what pins of the AVR of the AVR go where. This changes from revision A to revision B of the board and so on. As such, the header file is called "rev_a.h" or "rev_b.h" etc...

This header file needs to be included in the source file for the ethernet driver and the source file for the main code (that links to the ethernet driver). This means I need to change two files if I want to compile for a different revision of the board.

Is there any way to have it in one place? I tried to define it in the makefile and pass it using the cdefs line like so:

BOARD_REVISION = rev_a.h
CDEFS = -DF_CPU=$(F_CPU)UL -DBOARD_REVISION=$(BOARD_REVISION)

And then I have it as an include in my c files:

#include "BOARD_REVISION"

Problem is, I get a compile error: "error: BOARD_REVISION: No such file or directory" I've tried adding commas, quotes and so on in various places but no joy.

Anyone know how to handle this?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why wouldn't you just have:

pins.h:

#if BOARD_REVISION == 1
 #include "rev_a.h"
#elif BOARD_REVISION == 2
 #include "rev_b.h"
#elif ....

Then main.c and ethernet.c just always

#include "pins.h"

and you just just 1/2/3/... in a -D in the Makefile passed in as $(BOARD_REVISION)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks. I did a simpler version of your suggestion. I just made a pins.h file that both source files point at. The contents of the pins.h file was simply:

#include "rev_a.h"

Now I just change pins.h to point at the new revision. It's an extra file but no big deal.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If that's the ONLY thing that changes between revisions then I guess it's as easy to edit that file as the Makefile or pass a parameter to "make" but if more things are controlled by the revision I'd continue to head in the direction you were originally proposing.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would prefer to edit the makefile if possible like I tried but the extra file works for now.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

fizgig wrote:
I've got a header file that defines what pins of the AVR of the AVR go where. This changes from revision A to revision B of the board and so on. As such, the header file is called "rev_a.h" or "rev_b.h" etc...

This header file needs to be included in the source file for the ethernet driver and the source file for the main code (that links to the ethernet driver). This means I need to change two files if I want to compile for a different revision of the board.

Is there any way to have it in one place? I tried to define it in the makefile and pass it using the cdefs line like so:

BOARD_REVISION = rev_a.h
CDEFS = -DF_CPU=$(F_CPU)UL -DBOARD_REVISION=$(BOARD_REVISION)

And then I have it as an include in my c files:

#include "BOARD_REVISION"

Problem is, I get a compile error: "error: BOARD_REVISION: No such file or directory" I've tried adding commas, quotes and so on in various places but no joy.

Anyone know how to handle this?

The preprocessor does not process the insides of quoted strings.
The line should be
#include BOARD_REVISION
In the make file, do what you need to do to define BOARD_REVISION to something that begins and ends with a quote.

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you want to get rid of the extra file and it only includes that one line why not just replace the original includes in both places with the code Cliff suggested you put in "pins.h"?
The symbols for the make file would then still work in the same manner and you'd not have the extra file...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Got it working. I had to have the double quotes inside single quotes:

Source file looks like:

#include BOARD_REVISION

Makefile has the lines:

BOARD_REVISION = '"rev_a.h"'
CDEFS += -DBOARD_REVISION=$(BOARD_REVISION)

Compiles fine now. Thanks all for the help.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A version control system with two branches a and b, and no messing with changing the makefile.

Stealing Proteus doesn't make you an engineer.