C++ 为什么“extern const int n;” 没有按预期工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14894698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:47:10  来源:igfitidea点击:

Why does "extern const int n;" not work as expected?

c++constdeclarationextern

提问by xmllmx

My project consists of only two source files:

我的项目只包含两个源文件:

a.cpp:

a.cpp:

const int n = 8;

b.cpp:

b.cpp:

extern const int n;

int main()
{
    // error LNK2001: unresolved external symbol "int const n" (?n@@3HB)
    int m = n; 
}

I know there are several methods to make it work; however, I just wonder WHY it does't work?

我知道有几种方法可以使它起作用;然而,我只是想知道为什么它不起作用?

回答by James Kanze

It's because constimplies internal linkage by default, so your "definition" isn't visible outside of the translation unit where it appears.

这是因为const默认情况下暗示了内部链接,因此您的“定义”在它出现的翻译单元之外不可见。

In this case, by far the best solution is to put the declaration (extern int const n;) in a header file, and include that in both a.cppand b.cpp. The linkage is determined by the first declaration the compiler sees, so the later definition in a.cppwill have the correct (external) linkage.

在这种情况下,目前最好的解决方案是将声明 ( extern int const n;) 放在头文件中,并将其包含在a.cpp和 中b.cpp。链接由编译器看到的第一个声明确定,因此后面的定义 a.cpp将具有正确的(外部)链接。

Alternatively, you can force the linkage in the definition:

或者,您可以强制定义中的链接:

extern int const n = 8;

Despite the extern, this is still a definition; anything with an initializer outside of a class definition is a definition.

尽管如此extern,这仍然是一个定义;任何在类定义之外带有初始化程序的都是定义。

回答by AProgrammer

constand constexprvariables in C++ have internal linkage (and thus aren't accessible in other compilation unit) if they aren't also declared extern(either in the definition or in a previous declaration).

const如果constexprC++ 中的变量和变量也未声明extern(在定义中或在先前的声明中),则它们具有内部链接(因此无法在其他编译单元中访问)。

In C, it isn't the case (well C hasn't constexpr) so your code is valid, and more you can put externon a definition.

在 C 中,情况并非如此(以及 C 没有constexpr),因此您的代码是有效的,并且您可以添加更多extern定义。

So if you want to write code which is both C and C++ (and the two declarations should probably come from the same header as James pointed out):

因此,如果您想编写既是 C 又是 C++ 的代码(并且这两个声明可能应该来自 James 指出的同一个标头):

// a.cpp
extern const int n;
const int n = 8;

// b.cpp
extern const int n;

int main()
{

    int m = n; 
}

if you don't

如果你不

// a.cpp
extern const int n = 8;

is also possible

也是可能的

回答by Gjordis

Declare it extern in a.cpp and just use without extern in b.cpp:

在 a.cpp 中将其声明为 extern 并在 b.cpp 中不使用 extern 使用:

a.h

extern const int n ;

a.cpp

a.cpp

#include "a.h"
...
const int n= 8

b.cpp:

b.cpp:

#include "a.h"
...


int main()
{        
    int m = n; 
}

回答by Arpit

To share a const object among multiple files, you must define the variable as extern.

To share a const object among multiple files, you must define the variable as extern.

To define a single instance of a const variable, we use the keyword extern on both its definition and declaration(s):

From these rules you just need to add the externkeyword in your definition. you already have it in declaration.

根据这些规则,您只需要extern在定义中添加关键字。你已经在声明中了。

回答by einpoklum

If the other answers here do not do the trick, it may be the case that you have your definitions in different namespaces... if the compilation passes, and you get an undefined symbollinker error:

如果此处的其他答案没有解决问题,则可能是您的定义位于不同的命名空间中……如果编译通过,并且出现undefined symbol链接器错误:

  • check the namespace of the undefined symbol; that's the effective namespace for the extern const int ndeclaration.
  • ensure that's your effective namespace where you make the const int n = 8definition.
  • 检查未定义符号的命名空间;这是extern const int n声明的有效命名空间 。
  • 确保这是您进行const int n = 8定义的有效命名空间。