创建全局变量会导致链接器错误 [英] creating global variables causes linker error

查看:28
本文介绍了创建全局变量会导致链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MFC 应用程序 AVT_testapp,并且在头文件 (AVT_testappDlg.h) 中,我试图在所有函数、类等之外创建一个变量,以使其成为全局变量.每当我尝试这样做时(比如我尝试 int x = 7),我都会收到错误消息:

I have an MFC application AVT_testapp, and in the header file (AVT_testappDlg.h) I am trying to create a variable outside of all functions, classes, etc. in order to make it global. Whenever I try to do this though (say I try int x = 7), I get the error:

1>AVT_testappDlg.obj : error LNK2005: "int x" (?x@@3HA) already defined in 
    AVT_testapp.obj
1>....inx64DebugAVT_testapp.exe : fatal error LNK1169: one or more 
    multiply defined symbols found

我在 google 上找到的所有内容都说只需添加标头保护". AVT_testappDlg 有 6 个#include,每个都有标头保护.

Everything I have found on google says "just add header guards". AVT_testappDlg has 6 #include's, and each of them has header guards.

在创建全局变量时还有什么可能导致这些错误?

What else could be causing these errors when creating global variables?

这是我的头文件的开头,

Here is the beginning of my header file,

#pragma once

#include "../../src/CoreUtils/nierr.h"
#include "....srcCoreUtilsStringHelpers.h" //includes windows.h
#include "afxwin.h"
#include "afxcmn.h"
#include "IFrameObserver.h"
#include "c:Program Files (x86)Microsoft SDKsWindowsv7.0AIncludeGdiPlusHeaders.h"
//#include <fstream>
//#include <windows.h>

int x = 7;

using namespace AVT::VmbAPI;


//////////////////////////////////////////////////////////////////////////
//////////  MyObserver class   ///////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
class MyObserver : public IFrameObserver
{
private:
    MyObserver( MyObserver& );

    MyObserver& operator=( const MyObserver& );    

public:

    VmbUchar_t* imageData;

            //...
            //...
            //...
            //...

//that's the end of the relevant stuff

推荐答案

您不能在标题中的命名空间级别定义变量.一般来说,最好不要有全局变量,但如果需要,您应该只在标头中提供 声明,并在单个 .cpp 中提供定义:

You cannot define variables at namespace level in a header. In general it is best not to have global variables, but if you need to you should provide only a declaration in the header and the definition in a single .cpp:

//header
extern int i;

//cpp
int i;

您的代码问题与标头保护无关.标头保护确保标头在每个翻译单元中仅解析一次.缺少标头保护会导致 compiler 错误,编译器会看到,例如一个类,在预处理后在同一个翻译单元中定义了多次.在您的情况下,错误是链接器错误 LNK2005,这意味着在多个翻译单元中定义了相同的符号(在您的情况下,在每个翻译单元中都包含带有定义的标题).

The problem with your code is not related to header guards. Header guards ensure that a header is parsed only once in each translation unit. Lack of header guards causes compiler errors, where the compiler sees, say for example a class, defined multiple times in the same translation unit after preprocessing. In your case the error is a linker error LNK2005, and it means that the same symbol was defined in multiple translation units (in your case in each translation unit that includes the header with the definition).

这篇关于创建全局变量会导致链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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