简体   繁体   English

C中目标文件中具有多个定义的链接错误

[英]Linkage error with multiple definition in object file in C

i have a configuration file to define some of the functions and macros . 我有一个配置文件来定义一些功能和宏。

my_config.cfg 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 . 我将这个.cfg文件包含在另一个文件中以更新值,并且还定义了另一个函数来检查运行状态。

sample1.c 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 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 ? 有什么帮助吗?

将您的cfg文件文件包装为条件预处理程序指令,例如#ifndef,#define #endif https://gcc.gnu.org/onlinedocs/gcc-3.0.1/cpp_4.html

The file you're including, my_config.cfg , contains a definition for my_array . 您要包含的文件my_config.cfg包含my_array定义 That means that each source file that includes it contains a copy of my_array . 这意味着每个包含它的源文件都包含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: 因此,使用以下定义创建my_config.c

#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 : 并将以下声明放入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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM