重新访问头文件:extern与static [英] header files revisited: extern vs. static

查看:61
本文介绍了重新访问头文件:extern与static的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


extern关键字可用于C和C ++

来分享文件之间的全局变量

在头文件中声明变量并且

仅在两个文件中的一个中定义它。

例如,如果未声明x

extern在下面的头文件中

链接器会抱怨x

被多重定义。


------ --------------

// main.cpp


#include" hello.h"


int x;


int main(){


x = 0;


你好();


}

------------------- -

// hello.h


#ifndef HELLO_H

#define HELLO_H


extern int x;


void hello();


#endif // HELLO_H

- ------------------

// hello.cpp


#include< iostream>

#include" hello.h"

void hello(){


std :: cout<< x<< std :: endl;


}

------------------------ ----------

输出:0

--------------------- -------------


但是,我不太明白

如何在标题中使用关键字static

文件允许我们在头文件中定义变量

,并在多个文件中包含

头文件。

毕竟,当加载时,全局变量

和静态变量在

计算机程序的静态数据段中放置

/>
进入内存所以

的区别是什么?另外,为什么

1仍然打印在

第二个案例中

为0?


--------------------

// main.cpp


#include" ; hello.h"


int main(){


x = 0;


你好();


}

--------------------

// hello.h


#ifndef HELLO_H

#define HELLO_H


static int x = 1 ;


void hello();


#endif // HELLO_H

------- -------------

// hello.cpp


#include< iostream>

#include" hello.h"


void hello(){


std :: cout<< x<< std :: endl;


}

------------------------ ----------

输出:1

--------------------- -------------


谢谢,


JG

Hello,

The extern keyword can be used in C and C++
to share global variables between files by
declaring the variable in header file and
defining it in only one of the two files.
For example, if x were not declared
extern in the header file below then
the linker would complain about x
being multiply defined.

--------------------
// main.cpp

#include "hello.h"

int x;

int main() {

x = 0;

hello();

}
--------------------
// hello.h

#ifndef HELLO_H
#define HELLO_H

extern int x;

void hello();

#endif // HELLO_H
--------------------
// hello.cpp

#include <iostream>
#include "hello.h"

void hello() {

std::cout << x << std::endl;

}
----------------------------------
output: 0
----------------------------------

However, I don''t quite understand how
using the keyword static in the header
file allows us to define the variable
in the header file and include the
header file in multiple files.
After all, both global variables
and static variables are placed
in the static data segment of a
computer program when loaded
into memory so what is the
difference? Also, why is
1 still being printed in
the second case instead
of 0?

--------------------
// main.cpp

#include "hello.h"

int main() {

x = 0;

hello();

}
--------------------
// hello.h

#ifndef HELLO_H
#define HELLO_H

static int x = 1;

void hello();

#endif // HELLO_H
--------------------
// hello.cpp

#include <iostream>
#include "hello.h"

void hello() {

std::cout << x << std::endl;

}
----------------------------------
output: 1
----------------------------------

Thanks,

JG

推荐答案

John Goche写道:

< snip>
John Goche wrote:
<snip>

但是,我不太明白

如何在标题中使用关键字static

文件允许我们在头文件中定义变量

并在多个文件中包含

头文件。

毕竟,两个全局变量

和静态变量在

计算机程序的静态数据段中被加载

到内存中所以什么是

的区别?另外,为什么

1仍然打印在

第二种情况下

为0?
However, I don''t quite understand how
using the keyword static in the header
file allows us to define the variable
in the header file and include the
header file in multiple files.
After all, both global variables
and static variables are placed
in the static data segment of a
computer program when loaded
into memory so what is the
difference? Also, why is
1 still being printed in
the second case instead
of 0?



关键字''static''声明一个具有内部链接的对象。

#include只是将标题的内容复制并粘贴到

源代码中。这意味着您刚刚定义了两个不同的对象与

内部链接 - 每个翻译单元中有一个。可能不是你想要的b $ b,是吗?


问候,

Bart。

The keyword ''static'' declares an object with internal linkage. The
#include just copies and pastes the contents of the header into the
source code. This means you''ve just defined TWO different objects with
internal linkage - one in each of your translation units. Probably not
what you intended, is it?

Regards,
Bart.


John Goche写道:
John Goche wrote:

但是,我不太明白

如何使用标题中的关键字static

文件允许我们在头文件中定义变量

,并在多个文件中包含

头文件。 br />
毕竟,全局变量

和静态变量都放在

计算机的静态数据段中加载程序

进入内存所以

的区别是什么?
However, I don''t quite understand how
using the keyword static in the header
file allows us to define the variable
in the header file and include the
header file in multiple files.
After all, both global variables
and static variables are placed
in the static data segment of a
computer program when loaded
into memory so what is the
difference?



非静态全局变量具有外部链接,链接器可以通过其名称识别变量
并从不同对象中解析其用法

个文件。静态的是内部的,他的名字不会被导出,并且可以仅使用来自同一目标文件的
。如果您有两个静态变量,

在不同的翻译单元中具有相同的名称,则它们是两个不同的
变量。结果是相同的,如果你把它放在头文件或包含该标题的cpp的每个
中,头文件就像某种方式一样

自动复制和放大器;粘贴。


编译器可以使用与此

解释中所假设的不同构建模型,但结果必须相同。


-

Salu2

Non static global variables have external linkage, the linker can identify
the variables by his name and resolve his usage from different object
files. The static ones are internal, his names are not exported and can be
used only from the same object file. If you have two static variables with
the same name in different translations units, they are two different
variables. The result is the same if you put it in a header file or in each
of the cpp that include that header, header files are just like some way of
automated copy&paste.

A compiler can use a different build model that the supposed in this
explanation, but the result must be equivalent.

--
Salu2


JuliánAlbo写道:
Julián Albo wrote:

[..]

非静态全局变量有外部链接,[..]
[..]
Non static global variables have external linkage, [..]



Nit-pick:..除非他们被宣布为''const''。

Nit-pick: .. unless they are declared ''const''.


这篇关于重新访问头文件:extern与static的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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