I have a problem with defining my custom struct object as global using the extern keyword so that I might use it in more source files. I tried a number of variations, but so far, I could not build my project without errors. This is what I have so far:
main.h
#ifndef _MAIN_H_
#define _MAIN_H_
#include "pins.h"
#include "bluetooth.h"
const pin P0_21;
const pin P0_22;
const pin P0_27;
#endif
bluetooth.h
#ifndef _BLUETOOTH_H_
#define _BLUETOOTH_H_
#include "pins.h"
void EnableBT(void);
const pin BT_ENABLE;
const pin P2_0;
#endif
pins.h
#ifndef _PINS_H_
#define _PINS_H_
typedef struct
{
int port;
int pin;
int jump_phase;
int jump_counter;
} pin;
extern const pin P0_21;
extern const pin P0_22;
extern const pin P0_27;
extern const pin BT_ENABLE;
extern const pin P2_0;
extern const pin P2_2;
extern const pin P2_3;
extern const pin P2_7;
void InitPins(void);
#endif
main.c
#include "main.h"
int main()
{
InitPins();
/* Using pins from main.h */
}
bluetooth.c
#include "bluetooth.h"
void EnableBT()
{
/* Use pins in bluetooth.h */
}
pins.c
#include "pins.h"
void InitPins()
{
pin P0_21 = {0,21,0,0};
pin P0_22 = {0,22,0,0};
pin P0_27 = {0,27,0,0};
pin BT_ENABLE = {0,10,0,0};
pin P2_0 = {2,0,0,0};
pin P2_2 = {2,2,0,0};
pin P2_3 = {2,3,0,0};
pin P2_7 = {2,7,0,0};
}
When I remove the references to extern pins BT_ENABLe and P2_0 in bluetooth.h, the compiler says, that these objects referenced from bluetooth.c are not defined. But when I leave them, the compiler says they are multiply defined by bluetooth.o and main.o.
I have no more ideas how to change my code in order to make it work. Thanks for any advice.