简体   繁体   English

访问库中可执行文件中定义的全局变量时重定义错误

[英]Redefinition error while accessing a global variable defined in executable from library

I have static library file( lib_XXX.a ) with global variable defined in it.我有 static 库文件( lib_XXX.a ),其中定义了全局变量。 I am trying to access the global variable in my executable( exe_XXX.o ).我正在尝试访问我的可执行文件( exe_XXX.o )中的全局变量。 Linker error is coming. Linker 错误即将到来。 Any help would be thankful.任何帮助将不胜感激。

Languae : c
OS : Ubuntu gcc compiler

Sample as follows示例如下

exe_xxx.o module has 2 files resource.h and main.c exe_xxx.o模块有 2 个文件resource.hmain.c

resource.h code as follows: resource.h代码如下:

#ifndef RESOURCE_H
#define RESOURCE_H

#define APL

extern const StructTest g_AplObjDef;

const StructTest g_AplObjDef = {
abc, def, ghi,
....
};

#endif //APL

main.c code as follows: main.c代码如下:

#include "resource.h"
....
....
....

lib_xxx.a has another main.c in it. lib_xxx.a还有另一个main.c Its sample code as follows:其示例代码如下:

#include "resource.h"

int main()
{
#if defined(APL)
     fun1(g_AplObjDef);
#endif
}

I suspect the reason is because resource.h included in both the main.c files.我怀疑原因是因为resource.h包含在main.c文件中。 I couldn't way to get rid of this.我无法摆脱这个。 Can anyone help?任何人都可以帮忙吗?

Error details: /lib_XXX.a(lib_XXX_a-main.o):(.data.rel.ro.local+0x40): `g_AplObjDef' が重複して定義されています /exe_xxx-main.o:(.data.rel.ro.local+0x260): ここで最初に定義されています错误详情:/lib_XXX.a(lib_XXX_a-main.o):(.data.rel.ro.local+0x40): `g_AplObjDef' がして定义されています /exe_xxx-main.o:(.data. rel.ro.local+0x260): ここで最初に定义されています

Above error is in Japanese.. 1st line says "Duplicate is defined".上面的错误是日文的。第一行说“定义了重复”。 2nd line says "Here it is defined"第二行说“这里定义”

This part:这部分:

const StructTest g_AplObjDef = {
    abc, def, ghi,
    ....
};

is a definition, and should not be in a header.是一个定义,不应在 header 中。 Move it to a .c file.将其移至.c文件。

The reason for this is that header files are textually inserted, so if a header has a definition, and is included from multiple translation units, the symbol will be defined multiple times, which is an error.这是因为 header 文件是文本插入的,所以如果 header 有定义,并且包含在多个翻译单元中,则符号将被定义多次,这是错误的。

Here you are defining a variable in the header在这里,您在 header 中定义一个变量

 const StructTest g_AplObjDef = 

You should only declare, which you did the line before.你应该只声明你之前做过的那一行。 This definition should go into a code file, accessing it will be possible by the knowledge provded by the declaration in the header.此定义应将 go 写入代码文件,通过 header 中的声明提供的知识可以访问它。 But the definition in the header will be done in each code file which includes it, which causes the redundant definition mentioned in the error message.但是 header 中的定义将在包含它的每个代码文件中完成,这导致错误消息中提到的冗余定义。

Moving the definition (as you now have it in the header, including the {...} into the libs code file should help.移动定义(因为您现在在 header 中拥有它,包括{...}到 libs 代码文件中应该会有所帮助。

Note that having two main() will probably get you into trouble, I only focus this answer on the double definition of the variable.请注意,拥有两个main()可能会给您带来麻烦,我只将这个答案集中在变量的双重定义上。

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

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