extern decleration intialization [英] extern decleration intialization

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

问题描述

我想知道什么是C编译器的预期行为当

初始化externration时。


当使用以下代码编译时gcc


//文件extern.c

int arr [3];

int a;

//文件extern.h

extern int arr [3] = {1,2};

extern int a = 4;


//文件main.c

#include" extern.h"

#include< stdio.h>

int main()

{

printf(" a =%d \ n",a);

返回0;

}


extern decleration意味着声明了一个变量,并没有为它分配内存

。那么,为什么这样的初始化工作和

编译器只会为它生成警告?


gcc警告

extern。 h:1:警告:'arr''初始化并声明`extern''

extern.h:2:警告:'a''初始化并声明`extern''


另一个案例是

//文件extern.c

int arr [3] = {1,2,3};

int a = 5;

//文件extern.h

extern int arr [3] = {1,2};

extern int a = 4;


//文件main.c

#include" extern.h"

# include< stdio.h>

int main()

{

printf(" a =%d \ n",a );

返回0;

}


在这种情况下,编译器会生成重新定义arr的错误

和a。

gcc警告和错误

extern.h:1:警告:'arr''初始化并声明`extern''

extern.h :2:警告:'a''初始化并声明`extern''

/tmp/ccobzB9O.o(.data+0x0):`arr'的多重定义

/tmp/ccypnIjR.o(.data+0x0):首先在这里定义

/tmp/ccobzB9O.o(.data+0xc):a的多重定义

/tmp/ccypnIjR.o(.data+0xc):首先在这里定义

collect2:ld返回1退出状态


现在,这个意味着对于这个extern decelaration一个内存是

分配。这是预期的吗?


问候

Tapeesh

I would like to know what is the expected behaviour of C compilers when
an extern decleration is intialized.

When the following code is compiled using gcc

//File extern.c
int arr[3] ;
int a ;
//File extern.h
extern int arr[3] = {1,2};
extern int a = 4;

//File main.c
#include "extern.h"
#include <stdio.h>
int main()
{
printf("a = %d\n",a);
return 0;
}

An extern decleration means that a variable is declared and no memory
is allocated for it. So, why does such an initialization work and the
compiler only generates a warning for it?

gcc warning
extern.h:1: warning: `arr'' initialized and declared `extern''
extern.h:2: warning: `a'' initialized and declared `extern''

Another case is
//File extern.c
int arr[3] = {1,2,3} ;
int a = 5;
//File extern.h
extern int arr[3] = {1,2};
extern int a = 4;

//File main.c
#include "extern.h"
#include <stdio.h>
int main()
{
printf("a = %d\n",a);
return 0;
}

In this case the compiler generates an error of redefinition of "arr"
and "a".

gcc warning and error
extern.h:1: warning: `arr'' initialized and declared `extern''
extern.h:2: warning: `a'' initialized and declared `extern''
/tmp/ccobzB9O.o(.data+0x0): multiple definition of `arr''
/tmp/ccypnIjR.o(.data+0x0): first defined here
/tmp/ccobzB9O.o(.data+0xc): multiple definition of `a''
/tmp/ccypnIjR.o(.data+0xc): first defined here
collect2: ld returned 1 exit status

Now, this means that for this extern decelaration a memory is
allocated. Is that what is expected ?

Regards
Tapeesh

推荐答案

Tapeesh写道:
Tapeesh wrote:
extern decleration意味着声明了一个变量并且没有为它分配内存。
An extern decleration means that a variable is declared and no memory
is allocated for it.




我总是采用extern方式:变量被声明为''某处

else''但是我想在这个文件中使用它。不:声明但没有记忆是分配的
。如果你尝试extern一个''unallocated''变量编译器

不会抱怨但链接器会抱怨类似对象

找不到。


我总是这样使用extern:


file1.c:

int myvariable = 100;

....

file2.c:

extern int myvariable; // 好极了!我可以访问文件1中的变量

....

file3.c:

extern int myvariable; // 好极了!我可以访问文件1中的变量

....

我认为你应该能够在file1中的任何地方初始化myvariable,

file2或者file3但它必须在某处实例化。在这种情况下

file1。另外,根据经验,将变量初始化两次

有时会给你一个警告。


我相信这里的C大师可以告诉你更多。



I''ve always tought extern means: the variable is declared ''somewhere
else'' but I want to use it in this file. Not: declared but no memory is
allocated. If you try to extern an ''unallocated'' variable the compiler
will not complain but the linker will complain something like object
not found.

I''ve always used extern like this:

file1.c:
int myvariable = 100;
....
file2.c:
extern int myvariable; // Yay! I can access a variable in file 1
....
file3.c:
extern int myvariable; // Yay! I can access a variable in file 1
....
I think you should be able to initialise myvariable anywhere in file1,
file2 or file3 but it has to be instantiated somewhere. In this case
file1. Also, from experience, initialising the variable twice will
sometimes get you a warning.

I''m sure the C gurus here can tell you more.




Tapeesh写道:

Tapeesh wrote:
我想知道C的预期行为是什么当外部化程序被初始化时编译器。

当使用gcc编译以下代码时

//文件extern.c
int arr [3 ];
int a;

//文件extern.h
extern int arr [3] = {1,2};
extern int a = 4;

//文件main.c
#include" extern.h"
#include< stdio.h>
int main()
{
printf(" a =%d \ n",a);
返回0;
}

extern decleration意味着声明了一个变量并且没有为它分配内存。那么,为什么这样的初始化工作和
编译器只会为它生成警告?
I would like to know what is the expected behaviour of C compilers when
an extern decleration is intialized.

When the following code is compiled using gcc

//File extern.c
int arr[3] ;
int a ;
//File extern.h
extern int arr[3] = {1,2};
extern int a = 4;

//File main.c
#include "extern.h"
#include <stdio.h>
int main()
{
printf("a = %d\n",a);
return 0;
}

An extern decleration means that a variable is declared and no memory
is allocated for it. So, why does such an initialization work and the
compiler only generates a warning for it?




N869 [C99草案]:

6.9.2外部对象定义


语义

1如果对象的标识符声明具有

文件范围

和一个初始化程序,声明是标识符的外部

定义。

....



N869 [C99 draft]:
6.9.2 External object definitions

Semantics
1 If the declaration of an identifier for an object has
file scope
and an initializer, the declaration is an external
definition for the identifier.
....


sl*******@yahoo.com 写道:
sl*******@yahoo.com wrote:
我总是这样使用extern:

file1.c:
int myvariable = 100;
...

file2.c:
extern int myvariable; // 好极了!我可以访问文件1中的变量


file3.c:
extern int myvariable; // 好极了!我可以访问文件1中的变量
...
I''ve always used extern like this:

file1.c:
int myvariable = 100;
...

file2.c:
extern int myvariable; // Yay! I can access a variable in file 1
...

file3.c:
extern int myvariable; // Yay! I can access a variable in file 1
...




这是正常的方法:


file1.h:

extern int myvariable;

....


file1.c:

#include" file1.h"

int myvariable = 100;

....


file2。 c:

#include" file1.h" // 好极了!我可以访问文件1中的变量

....


file3.c:

#include" file1。 H" // 好极了!我可以访问文件1中的变量

....


定义任何外部对象后立即

是个好时机决定是否使其成为静态存储类。

如果没有,那么这可能是个好时机

来写它为它写一个外部声明。


-

pete



This the normal way to do that:

file1.h:
extern int myvariable;
....

file1.c:
#include "file1.h"
int myvariable = 100;
....

file2.c:
#include "file1.h" // Yay! I can access a variable in file 1
....

file3.c:
#include "file1.h" // Yay! I can access a variable in file 1
....

Immediately upon defining any external object
is a good time to decide whether to make it static storage class.
If not, then that might be a good time
to write to write an extern declaration for it.

--
pete


这篇关于extern decleration intialization的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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