1

I have a header file, let's call it foo.h. Inside of foo.h, I have the following code.

extern const size_t video_column_size;

In another file, let's call it first.c, I have the following code.

#include "foo.h"
video_column_size = 4;

My goal is for this value to be a single constant variable that will be shared across all classes that include the foo.h header.

When I compile and link, I get the following errors.

warning: data definition has no type or storage class
warning: type defaults to 'int' in declaration of 'video_column_size' [-Wimplicit-int]
conflicting type qualifiers for 'video_column_size'
note: previous declaration of 'video_column_size' was here: 
extern const size_t video_column_size;

From what I understand, the reason this is happening is because C defaults to the int type when there is no type declaration. However, I assumed that the definition of the variable would carried over from the header file. Where am I going wrong?

new_user
  • 11
  • 1

1 Answers1

2

Hey,

Here's the problem. In the header file you declared video_column_size as an extern variable, which did not create a variable (unlike regular const size_t video_column_size). Basically, extern keyword serves for resolving symbols on the linking stage, it does not allocate the memory associated with the symbol but only declares that the symbol exists somewhere else.

So, we move to the first.c and that's where the compiler kicks in. Since the assignment is placed out of any function block, it assumes that you are creating a new global variable and assigning to it. As no type was explicitly specified, it assumes that the new video_column_size variable declared in first.c is of type int and gives you an error since both your exported extern const size_t video_column_size and your new int video_column_size create different strong symbols with the same name. These two strong symbols cannot be resolved by the linker, and so you get the error from the compiler.

Hopefully, I managed to explain the problem. Here you can find more on the issue.

Paper_Lark
  • 68
  • 1
  • 11