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

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

问题描述

我有一个MFC应用程序AVT_testapp,并在头文件(AVT_testappDlg.h)我试图创建一个变量所有的函数,类等之外,以使其全局。每当我尝试这样做(比如我尝试 int x = 7 ),我得到的错误:

  1> AVT_testappDlg.obj:error LNK2005:已在
AVT_testapp.obj
1> .. \中定义的int x(?x @@ 3HA) .\bin \x64\Debug\AVT_testapp.exe:致命错误LNK1169:一个或多个
多重定义的符号找到

我在google上找到的所有内容都表示只是添加标题保护。 AVT_testappDlg有6个#include,




$ b

编辑:这是开始的时候的头文件

  #pragma once 

#include../../src /CoreUtils/nierr.h
#include..\..\src\CoreUtils\StringHelpers.h//包括windows.h
#includeafxwin.h
#includeafxcmn.h
#includeIFrameObserver.h
#includec:\Program Files(x86)\Microsoft SDKs\Windows \v7.0A\\ \\Include\GdiPlusHeaders.h
//#include< fstream>
//#include< windows.h>

int x = 7;

使用命名空间AVT :: VmbAPI;


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

MyObserver& operator =(const MyObserver&);

public:

VmbUchar_t * imageData;

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

这是相关内容的结束


解决方案

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

  // header 
extern int i;

// cpp
int i;

您的代码的问题与标题卫is无关。标题保护确保每个转换单元只解析一次标题。缺少标头保护程序会导致编译器错误,编译器会在预处理之后在同一个翻译单元中查看多次定义的类,例如一个类。在您的情况下,错误是链接器错误 LNK2005 ,这意味着在多个翻译单元中定义了相同的符号(在每个翻译单元中包括带定义的标题)。


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>..\..\bin\x64\Debug\AVT_testapp.exe : fatal error LNK1169: one or more 
    multiply defined symbols found

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?

EDIT: Here is the beginning of my header file,

#pragma once

#include "../../src/CoreUtils/nierr.h"
#include "..\..\src\CoreUtils\StringHelpers.h" //includes windows.h
#include "afxwin.h"
#include "afxcmn.h"
#include "IFrameObserver.h"
#include "c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include\GdiPlusHeaders.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

解决方案

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;

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天全站免登陆