0

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.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • 1
    Take a good look at [How do I use `extern` to share variables between source files in C?](https://stackoverflow.com/questions/1433204/) – Jonathan Leffler Oct 22 '15 at 06:47
  • 1
    You are declaring *new* local variables in `InitPins`. – Some programmer dude Oct 22 '15 at 06:48
  • 2
    The local variables in `InitPins()` bear only coincidental relationship to the names declared in the headers. The names in the headers are global variables; the ones in the function are only accessible by the function. Some source file (`.c` file) needs to define the variables outside any function. – Jonathan Leffler Oct 22 '15 at 06:49
  • Joachim, Johnathan: Thanks, I tought that any variable bearing the name of the extern variable would reference the extern variable itself. I defined the pin objects in pins.c outside any function and now it compiles. Thank you! – Tomáš 'Guns Blazing' Frček Oct 22 '15 at 07:01

1 Answers1

0

remove the lines

const pin P0_21;
const pin P0_22;
const pin P0_27;

and

const pin BT_ENABLE;
const pin P2_0;

from main.h and bluetooth.h and put them in pins.c just before definitions of InitPins(). AND Initialize the globals inline:

#include "pins.h"

const pin P0_21 = {0,21,0,0};
const pin P0_22 = {0,22,0,0};


void InitPins()
{
...

Remove these initializations in InitPins().

Alternatively, you can keep initializations in InitPins() but remove const keywords in pin declarations in pins.c file.

In other files that use these pins, include pins.h

GPS
  • 1,322
  • 1
  • 18
  • 40