多重定义...链接错误 [英] Multiple definition of ... linker error

查看:150
本文介绍了多重定义...链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个特殊的文件:的config.h

I defined a special file: config.h

我的项目也有文件:

t.c, t.h
pp.c, pp.h
b.c b.h
l.cpp

和#包括:

在T.C:

    #include "t.h"
    #include "b.h"
    #include "pp.h"
    #include "config.h"

在b.c:

    #include "b.h"
    #include "pp.h"

在pp.c:

    #include "pp.h"
    #include "config.h"

在l.cpp:

    #include "pp.h"
    #include "t.h"
    #include "config.h"

有没有包括在我的指令*的.h 文件,只在 *。ç文件。我在config.h中定义的:

there are no include directives in my *.h files, only in *.c files. I defined this in config.h:

const char *names[i] =
        {
            "brian", "stefan", "steve"
        };

和需要l.cpp,T.C,pp.c但是我得到这个错误该数组:

and need that array in l.cpp, t.c, pp.c but Im getting this error:

pp.o:(.data+0x0): multiple definition of `names'
l.o:(.data+0x0): first defined here
t.o:(.data+0x0): multiple definition of `names'
l.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [link] Error 1

我有包括每个 *的.h 文件我在项目中使用警卫。任何解决这个帮助?

I have include guards in every *.h file I use in my project. Any help solving this?

推荐答案

不要在标题定义变量。放在头的声明和定义在.c文件中的一个。

Don't define variables in headers. Put declarations in header and definitions in one of the .c files.

在config.h中

extern const char *names[];

在某些.c文件:

const char *names[] =
    {
        "brian", "stefan", "steve"
    };

如果你把一个头文件中的全局变量的定义,那么这个定义会去,包括这个头的每个.c文件,你会得到多个定义错误,因为varible可能被多次声明,但可以定义只有一次。

If you put a definition of a global variable in a header file, then this definition will go to every .c file that includes this header, and you will get multiple definition error because a varible may be declared multiple times but can be defined only once.

这篇关于多重定义...链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆