arc_lupus
arc_lupus

Reputation: 4114

Usage of functions across multiple include files

I am currently programming a µC in C, and for making things easier to read I separated all different connection functions (like SPI, TWI, GPIO) in separate function-/include-files. Thus I have now two files for all functions regarding TWI, two files for GPIO aso.. Now I want to use functions for SPI in my GPIO-functions, and the other way round. The easiest way would be to put simply all functions in a big file, but I want to avoid that in order to increase readibility. But I also can not include the GPIO-include file into the SPI-include file, after I want to use the functions from both files in the other file, too.
Thus, what is the best way to cross-use functions from two different files in C?

Upvotes: 0

Views: 45

Answers (1)

Amit
Amit

Reputation: 46323

How to use headers:

a.h

int DoA();
int DoA2();

a.c

#include <b.h>
int DoA()
{
   return 1 + DoB();
}
int DoA2()
{
   return 2;
}

b.h

int DoB();

b.c

#include <a.h>
int DoB()
{
   return 1 + DoA2();
}

And with safe guards, you'd do:

a.h

#ifndef _INCL_A
#define _INCL_A

int DoA();
int DoA2();

#endif

Upvotes: 2

Related Questions