initialized and declared 'extern' [enabled by default]

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

initialized and declared 'extern' [enabled by default]

9,663件の閲覧回数
randenewberry
Contributor IV

We are using Coderwarior 10.5.

We are porting a Coldfire program to a K20 MK20DX128vlh5.

The GCC compiler complains of initialized and declared 'extern' [enabled by default] for all my external variables.

As an example

In the .h file I have

extern const char stCR[2];

In the .c file I have

extern const char stCRLF[]={"\r\n\0"};

This works fine for the Coldfire compiler but the GCC compiler gives a warning for every one of my variables declared as external.

What is the proper syntax to do the same thing in GCC?

Or can this warning be safely ignored?

Thanks

ラベル(1)
0 件の賞賛
返信
2 返答(返信)

6,529件の閲覧回数
pgo
Senior Contributor V

Hi Rande,

You usually associate extern with the declaration of a variable i.e. indicating that it exists somewhere.

You define the variable without the extern.

...but

if you initialise a variable it is always considered a definition irrespective of the extern.

So in summary:

In header file (.h)  where you are declaring the variable use

extern const char stCR[2];


In the actual file (.c file) where you are defining (creating) use

const char stCR[2] = "ab";

This also applies even if not initialising the variable e.g not a constant.


It is only considered a warning but it is best to adopt the suggested approach.


PS.  I presume the inconsistencies in the example in your question are a typo? Otherwise they are different variables.

bye


0 件の賞賛
返信

6,529件の閲覧回数
mjbcswitzerland
Specialist V

Hi

You will get complains due to

extern const char stCRLF[]={"\r\n\0"};

when you have

extern const char stCR[2];

since it is "declared" extern but "initialised" extern. GCC warns as default and other compilers may do, depending on the level of warning/error settings.

There is no need to initialise extern when already declared extern so I would remove the extern for all global variables since these 'should' be declared in a header anyway, otherwise they shouldn't be global and so should be static. The warning "could" be ignored since it doesin't cause a problem but people will tend not to trust code with warnings so probably best to drop the externs where initialsiation takes place....

Regards

Mark

0 件の賞賛
返信