0

I am developing a program to read NFC cards, and I have a header file, source.h, holding all the variables for use in it's related source code, source.c. A sample is as shown below:

#ifdef __cplusplus
extern "C" {
#endif

int SDA_SUPPORT_PRESENT = 0;
int DDA_SUPPORT_PRESENT = 0;
int CDA_SUPPORT_PRESENT = 0;
int CARDHOLDER_VERIFICATION_PRESENT = 0;
...

The source code, source.c, holds the methods utilizing the above defined variables. A sample is as shown:

#include <source.h>

extern void translateError(uint8 error, int slot) //one of the methods
{
    TP_DbgSerialPrn("\n\rDevice Error: ");
    switch(error)
    {
...

I also have a source file, CardReader.c, which calls the methods included in source.c, and which has a related header file, CardReader.h. The issue is, when I include the source.h file in the CardReader.h file, i get the following error:

../CardReader.o:(.bss+0x12b4): first defined here
../source.o:(.bss+0x12b8): multiple definition of `SLOT_NUMBER'
../CardReader.o:(.bss+0x12b8): first defined here
../source.o:(.data+0x49): multiple definition of `LISTED_APPLICATION_IDS'
../CardReader.o:(.data+0x49): first defined here
../source.o:(.data+0xc9): multiple definition of `LISTED_APPLICATION_IDS_LENGTH'

All the rest of the error messages are of the same type. The source.h file is included as indicated in CardReader.h:

#include <TPCardReader.h> 
#include <source.h>

#ifdef __cplusplus
extern "C" {
#endif
...

with the path variables set properly, so it can be found, and then called the CardReader.h file as usual in CardReader.c. My question is why is that error coming up, yet I have only defined each of the specified variables once in source.h? Is there something am missing or not doing or do I not understand the error?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Peter
  • 648
  • 7
  • 26

1 Answers1

1

The variables should not be defined in the header file.

Instead in the header file you should have

extern int SDA_SUPPORT_PRESENT;

Then in the source (.c) file you should have

int SDA_SUPPORT_PRESENT = 0;

This will ensure you only have one definition of the variable

But then again global variables are a bad idea

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • As I have read from the other duplicate questions, you cannot even specify array sizes in the header file. What if I want to have an array of size 16 but only specify the first 3 indices values? e.g. int sample[16] = {0, 1, 2}. How do I do that? – Peter Jan 18 '15 at 07:16