There are 2 files libtest.h and libtest.c
libtest.h
int a;
libtest.c
#include "libtest.h"
a = 10;
When I compile libtest.c
cc -c libtest.c
libtest.o is generated but I get this warning. What is the reason ?
libtest.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
a = 10;
^
If I do the same thing, but inside some function in libtest.c
#include "libtest.h"
int foo()
{
a = 10;
return(0);
}
cc -c libtest.c
compiles without any warnings. What is the difference ?
You cannot assign a variable outside a function, only define and initialise. Syntactically your a = 10;
is a re-definition of a
, which presumably subsequent errors make clear.
To provide static initialisation of a
, in the header you should have;
extern int a ;
and then in the C source;
#include "libtest.h"
int a = 10 ;