简体   繁体   English

多包含一个.h文件

[英]Multi Including a .h File

In an .h file, I am declaring a global variable as: 在.h文件中,我将全局变量声明为:

#pragma data_seg(".shared")
#ifndef DEF_VARX
#define DEF_VARX
int VARX=0;
#endif /*DEF_VARX*/
#pragma data_seg()
#pragma comment(linker, "/SECTION:.shared,RWS")

However if I include this file in multiple cpp files, when I try to compile, I get " error LNK2005: "int VARX" (?VARX@@3HA) already defined in Dll.obj" error. 但是,如果我将此文件包含在多个cpp文件中,则在尝试编译时,将收到“错误LNK2005:Dll.obj中已定义的“ int VARX”(?VARX @@ 3HA)”错误。 If I include in only one cpp file, no problem is encountered. 如果我仅包含一个cpp文件,则不会遇到任何问题。

Isn't #IFNDEF.... check enough for preventing this? #IFNDEF ....检查是否足以防止这种情况发生? Am I missin something? 我想念什么吗?

The reason of this behavior is, that you compile the line 此行为的原因是,您编译该行

int VARX=0;

into each .obj file. 放入每个.obj文件。 Thats OK for compiling, but upon linking the symbol becomes multiply defined, which is illegal. 可以编译,但是在链接时,符号被多重定义,这是非法的。 Using 使用

extern int VARX;

in the header file, and 在头文件中,以及

int VARX=0;

in one (and only one) source file resolves this problem. 一个(只有一个)源文件中的文件解决了此问题。

I think you're supposed to forward declare the variable in the .h and later define it in its shared section in a .cpp, something like: 我认为您应该在.h中转发声明该变量,然后在.cpp的共享部分中对其进行定义,如下所示:

// in a header file
#pragma once
extern int VARX;

// in a .cpp
#pragma data_seg(".shared")
int VARX=0;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.shared,RWS")

The problem is that is that you prevent multiple inclusion of the file for a given translation unit. 问题在于,对于给定的翻译单元,您可以防止文件的多次包含。 (for a given say cpp file) (对于给定的说说cpp文件)

But if several of your cpp include this VARX.H, then you'll have more than one definition for this variable. 但是,如果您的多个cpp都包含此VARX.H,则此变量将具有多个定义。

Instead, you should only declare the variable in the .H file, but initialize it to 0 in only one location. 相反,您只应在.H文件中声明变量,而仅在一个位置将其初始化为0。

Yes, you're missing the extern keyword. 是的,您缺少extern关键字。

In your header file, use: 在头文件中,使用:

extern int VARX; extern int VARX;

In a source file, actually declare space for the variable: 在源文件中,实际上为变量声明空间:

int VARX = 0; int VARX = 0;

ifdef prevents it for a separe object file. ifdef禁止将其用作Separe目标文件。 When the header is include in several source (cpp) files, VARX will be dedfined in all of them. 当标头包含在多个源(cpp)文件中时,VARX将在所有这些文件中定义。 Consider declaring it as extern in header file, and initialize in one cpp file. 考虑在头文件中将其声明为extern,并在一个cpp文件中进行初始化。

The problem is you must be including the file in multiple compilation units. 问题是您必须将文件包含在多个编译单元中。 Let's say you have a.cpp and b.cpp. 假设您有一个a.cpp和b.cpp。 Both include your header file. 两者都包含您的头文件。 So the compiler will compile (and pre-process) separately, so for both files, DEF_VARX is not yet defined. 因此,编译器将分别编译(和预处理),因此对于这两个文件,尚未定义DEF_VARX。 When you go to link the to object files together, the linker notices that there is a name collision. 当您将链接到目标文件链接在一起时,链接器会发现名称冲突。

As others have suggested, the solution would be to declare it 'extern', then place the actual value in a cpp file, so it only is compiled once, and linked to everything without name collisions. 正如其他人所建议的那样,解决方案是将其声明为“ extern”,然后将实际值放置在cpp文件中,因此它仅被编译一次,并链接到所有内容而不会发生名称冲突。

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

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