Linkage error with multiple definition in object file in C

211 Views Asked by At

i have a configuration file to define some of the functions and macros .

my_config.cfg

#define index_zero  0
#define index_one    1 
#define index_two    2
#define index_three  3
 
unit8 index;

typedef struct
{
   const UINT16   first_value;
   const  UINT16   second_value;
   UINT16   updated_value;
}test;
 
 
test   my_array[3];
 
test   my_array[3] = 
{
    {0, 22, 0},
    {0, 44, 0},
    {0, 33, 0}
};

static void set_zero_value (void)
{
    for(i=0;i<3;i++)
    {
       my_array[i].updated_value = first_value;
    }
}

static void set_temp_value (void)
{
    for(i=0;i<3;i++)
    {
       my_array[i].updated_value = second_value;
    }
}

static UINT16 update_value (uint8 index)
{

     return  (my_array[index].updated_value);

}

i am including this .cfg file in another file for updating the values and also i am defining another function to check the running status .

sample1.c

#include "my_config.cfg"
#include "sample1.h"

boolean isrunning(void)
{
  if(condition1)
   return true ;
  else 
    return false ;
}
set_zero_value();
set_temp_value();

and also in another file , i am checking whether it is running or not , if running , i am updating some of the values from my config file .

sample2.c

#include "my_config.cfg"
#include "sample1.h"
#include "sample2.h"

if(isrunning())
{
   UINT16 first = update_value(index_zero);
   UINT16 second = update_value(index_one);
   UINT16 third = update_value(index_two);
}

After compiling the code , I am getting the error at linkage time

multiple definition of  `my_array` in object file in sample2.o and sample1.o

and

multiple definition of  `index` in object file in sample2.o and sample1.o

i don't know why i am getting this error at the time of linkage , i have to include both header files to access those functions . Any help ?

2

There are 2 best solutions below

2
On

wrap your cfg file file a conditional preprocessor directive like #ifndef, #define #endif https://gcc.gnu.org/onlinedocs/gcc-3.0.1/cpp_4.html

0
On

The file you're including, my_config.cfg, contains a definition for my_array. That means that each source file that includes it contains a copy of my_array. Then when these files are linked, you get an error because each compiled source file contains a copy.

You need to move the definition of the array to a separate source file, and put a declaration of the array in your header file.

So create my_config.c with the following definitions:

#include "my_config.cfg"

test my_array[3] = 
{
    {0, 22, 0},
    {0, 44, 0},
    {0, 33, 0}
};

void set_zero_value (void)
{
    for(i=0;i<3;i++)
    {
       my_array[i].updated_value = first_value;
    }
}

void set_temp_value (void)
{
    for(i=0;i<3;i++)
    {
       my_array[i].updated_value = second_value;
    }
}

UINT16 update_value (uint8 index)
{

     return  (my_array[index].updated_value);

}

And put the following declarations in my_config.cfg:

#ifndef MY_CONFIG_CFG
#define MY_CONFIG_CFG

#define index_zero  0
#define index_one    1 
#define index_two    2
#define index_three  3

unit8 index;

typedef struct
{
   const UINT16   first_value;
   const  UINT16   second_value;
   UINT16   updated_value;
} test;

extern test  my_array[3];

void set_zero_value (void);
void set_temp_value (void);
UINT16 update_value (uint8 index);

#endif