如何通过多次包含头文件来避免变量的多重定义 [英] How to avoid multiple definition of a variable by multiple inclusion of a header file

查看:235
本文介绍了如何通过多次包含头文件来避免变量的多重定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

容易描述的问题:


在9.3下使用g ++版本3.3.5,


bla.h:

-----------

#ifndef myTEST

#define myTEST

ZFSInt测试;

#endif

------------


在链接时导致错误...

application.o(.bss + 0x0):/ path_here / application.cpp:19:多个

定义`test''

app .o(.bss + 0x0):/ path_here / app.cpp:17:首先在这里定义

....每次调用#include" bla.h"

:(


显然#ifndef只能在编译时工作,但不知何故导致

在链接时发生错误。任何解决方案?我想要变量测试

可用于所有模块#include" bla.h"


问候,


Lars Uffmann

Easily described problem:

Using g++ version 3.3.5 under suse 9.3,

bla.h:
-----------
#ifndef myTEST
#define myTEST
ZFSInt test;
#endif
------------

leads to an error at link time...
application.o(.bss+0x0):/path_here/application.cpp:19: multiple
definition of `test''
app.o(.bss+0x0):/path_here/app.cpp:17: first defined here
....for EVERY call of #include "bla.h"
:(

Apparently the #ifndef only works at compile time, but somehow causes
an error at linktime. Any solution? I want the variable "test"
available in all modules that #include "bla.h"

Regards,

Lars Uffmann

推荐答案



< la ********** @ rwth-aachen.dewrote in message

news:11 **********************@m79g2000cwm.googlegr oups.com ...

<la**********@rwth-aachen.dewrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...

轻松描述问题:


在9.3下使用g ++版本3.3.5,


bla.h:

- ---------

#ifndef myTEST

#define myTEST

ZFSInt测试;

#endif

------------


在链接时导致错误...

application.o(.bss + 0x0):/ path_here / application.cpp:19:多个

定义`test''

app.o(.bss + 0x0):/ path_here / app.cpp:17:首先在这里定义

...每次调用#include" bla.h"

:(


显然#ifndef只能在编译时工作,但不知何故导致

在链接时发生错误。有解决方案吗我希望变量test

可用于所有模块#include" bla.h"
Easily described problem:

Using g++ version 3.3.5 under suse 9.3,

bla.h:
-----------
#ifndef myTEST
#define myTEST
ZFSInt test;
#endif
------------

leads to an error at link time...
application.o(.bss+0x0):/path_here/application.cpp:19: multiple
definition of `test''
app.o(.bss+0x0):/path_here/app.cpp:17: first defined here
...for EVERY call of #include "bla.h"
:(

Apparently the #ifndef only works at compile time, but somehow causes
an error at linktime. Any solution? I want the variable "test"
available in all modules that #include "bla.h"



首先,复制定义将上面的测试变成一个实现文件,你将

总是链接(其中一个包含头文件,其中定义为

ZFSInt所在的)。然后,使用

关键字extern在blah.h中加上上述声明。 (其次是至少一个空格)。这告诉编译器

在其他地方寻找实际的测试声明,允许其他单位通过这个头看到变量,并阻止链接器看到

多个定义。


-Howard

First, copy the definition of test above into an implementation file you''ll
always link with (one which includes the header file where the definition of
ZFSInt resides). Then, preface the above declaration in blah.h with the
keyword "extern" (followed by at least one space). That tells the compiler
to look elsewhere for the actual declaration of test, allows other units to
see the variable through this header, and prevents the linker from seeing
multiple definitions.

-Howard


Howard写道:
Howard wrote:

首先,将上面测试的定义复制到一个实现文件中,你将

始终链接(其中一个包含头文件,其中定义为

ZFSInt驻留)。然后,使用

关键字extern在blah.h中加上上述声明。 (其次是至少一个空格)。
First, copy the definition of test above into an implementation file you''ll
always link with (one which includes the header file where the definition of
ZFSInt resides). Then, preface the above declaration in blah.h with the
keyword "extern" (followed by at least one space).



问题解决了 - 谢谢!我将实际声明放在cpp文件中

属于

到blah.h类定义头文件。


Any链接到一个网站,解释#ifdef / #ifndef的这种行为?


最好的问候,


Lars

Issue solved - thank you! I put the actual declaration in the cpp-file
belonging
to the blah.h class definition header file.

Any link to a website explaining this behaviour of #ifdef / #ifndef ?

Best Regards,

Lars


la ********** @ rwth -aachen.de 写道:

Howard写道:
Howard wrote:

>首先,复制将上面的测试定义为一个实现文件,你将始终链接(一个包含ZFSInt定义所在的头文件)。然后,使用
关键字extern在blah.h中加上上述声明。 (其次是至少一个空格)。
>First, copy the definition of test above into an implementation file you''ll
always link with (one which includes the header file where the definition of
ZFSInt resides). Then, preface the above declaration in blah.h with the
keyword "extern" (followed by at least one space).



问题解决了 - 谢谢!我将实际声明放在cpp文件中

属于

到blah.h类定义头文件。


Any链接到一个网站,解释#ifdef / #ifndef的这种行为?


Issue solved - thank you! I put the actual declaration in the cpp-file
belonging
to the blah.h class definition header file.

Any link to a website explaining this behaviour of #ifdef / #ifndef ?



这不是预处理器问题,而是ODR(一个定义规则)。

That''s not a preprocessor issue, it''s the ODR (one definition rule).


这篇关于如何通过多次包含头文件来避免变量的多重定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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