関数とマクロの一部を定義する構成ファイルがあります。
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);
}
値を更新するために、この .cfg ファイルを別のファイルに含めています。また、実行ステータスをチェックする別の関数を定義しています。
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();
また、別のファイルで、実行中かどうかを確認しています。実行中の場合は、構成ファイルからいくつかの値を更新しています。
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);
}
コードをコンパイルした後、リンク時にエラーが発生します
multiple definition of `my_array` in object file in sample2.o and sample1.o
と
multiple definition of `index` in object file in sample2.o and sample1.o
リンク時にこのエラーが発生する理由がわかりません。これらの関数にアクセスするには、両方のヘッダー ファイルを含める必要があります。助けはありますか?