Header library setup

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

Say I have a program called a.c and a header and library files named header.h and header.c

In a subdir avr there are three subdir to avr named code, where a.c resides and include where header.h resides and lib where header.c resides.

avr --- code/a.c
|- header/header.h
|- lib/header.c

In a.c there is a line
#include "/home/avr/code/header.h" // I'm using ubuntu

In makefile the source file list is
SRC = $(TARGET).c /
header.c

When the header file is in a subdir below the code subdir this works.

What I would like to know how to do is place header.h and header.c in individual subdirs and a.c in yet another and have it all work. Is this the proper way to setup my library? If not, how? My brain is kind of dense, please give both an explanation and an example.

Thanks,
Mike

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

If you had

avr/a.c
avr/header.h
avr/header.c

Then in a.c you can just do

#include "header.h"

and have it find the file. If you had

avr/a.c
avr/header/header.h
avr/header/header.c

Then in a.c you could do

#include "header/header.h"

and in header.c

#include "header.h"

because header.h is in the same dir as header.c.

If you had

avr/code/a.c
avr/header/header.h
avr/lib/header.c

Then in a.c and header.c you'd need to use

#include "../header/header.h"

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

Alternatively, tell the preprocessor where to search for the header files, through the -I switch.

http://gcc.gnu.org/onlinedocs/gc...

JW

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

Quote:

Then in a.c and header.c you'd need to use

#include "../header/header.h"


And similarly the Makefile can refer to "../lib/header.c" to access the C file. (or explore the use of VPATH).