简体   繁体   English

C ++ extern关键字和全局变量

[英]C++ extern keyword and global variables

I have two files called file_utils.h and file_utils.cpp which contain some methods and variables which are used by different classes. 我有两个名为file_utils.h和file_utils.cpp的文件,它们包含一些由不同类使用的方法和变量。 Here's an example of how it looks: 以下是它的外观示例:

file_utils.h: file_utils.h:

namespace my_namespace
{
extern Foo* foo;
extern Bar* bar;

void my_function(Blah* blah);
}

file_utils.cpp file_utils.cpp

#include "file_utils.h"

void my_namespace::my_function(Blah* blah)
{
    foo = 0;    // undefined reference to my_namespace::foo
    bar = 0;    // undefined reference to my_namespace::bar
    //...
}

some_class.cpp some_class.cpp

#include "file_utils.h"

some_function()
{
    my_namespace::my_function(blah);
    this->foo = *my_namespace::foo; // will that work ok?
}

So the errors are in the comments. 所以错误在评论中。 If I remove the extern keyword I get multiple definition of my_namespace::foo error. 如果我删除extern关键字,我会得到multiple definition of my_namespace::foo错误的multiple definition of my_namespace::foo What is the problem? 问题是什么? Is that even a good idea from design standpoint or should I try to use a class with static members and methods instead? 从设计的角度来看,这是一个好主意,还是我应该尝试使用静态成员和方法的类?

The problem is that you only declared but not defined the variables. 问题是你只声明未定义变量。

You need to provide a definition in a single implementation file: 您需要在单个实现文件中提供定义:

file_utils.cpp file_utils.cpp

#include "file_utils.h"

//definition:
namespace my_namespace
{
   Foo* foo;
   Bar* bar;
}

//alternatively, to keep the same formatting you have
//Foo* my_namespace::foo;
//Bar* my_namespace::bar;

void my_namespace::my_function(Blah* blah)
{
    foo = 0;
    bar = 0;
    //...
}

To begin with, i would really recommend you to clarify yourself two differences: 首先,我真的建议你澄清自己两个不同之处:

  • Declaration vs definition 声明与定义
  • compiler vs linker 编译器与链接器

In your case you get linker error. 在您的情况下,您会收到链接器错误 Undefined reference means, that your code points somewhere, but the linker is unable to figure out where. 未定义的引用意味着您的代码指向某处,但链接器无法找出位置。

The extern keyword is used to suppress compiler error, when you use an object found somewhere in your code. 当您使用代码中某处找到的对象时,extern关键字用于抑制编译器错误。 In your example the linker tries to find instances of your classes but fails. 在您的示例中,链接器尝试查找类的实例但失败。 This is not commonly used with pointers (correct me if i'm wrong). 这不常用于指针(如果我错了,请纠正我)。 Therefore: 因此:

utils.h : utils.h

namespace mySpace{
    extern Foo foo; // notes that foo of type Foo will be used.

    void myFn();
}

utils.cpp : utils.cpp

#include "utils.h"

void mySpace::myFn(){
    foo.bar = 5; // changes a member of foo from globals.cpp
}

globals.cpp globals.cpp

Foo mySpace::foo; // this is the actual foo used.

If you want your question about some_class.cpp answered please provide the community with knowledge of what is conversions and what you want to achieve. 如果您想解答有关some_class.cpp的问题,请向社区提供有关conversions和您希望实现的内容的知识。

Judging from semantics I believe it to be a reference to a member of a class conversions . 从语义来看,我认为它是对类conversions成员的引用。

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

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