Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable defined in header file shared?

Tags:

c

header-files

In header file "foo.h", there is the following statement,

int foo;

In source file "a.c" and "b.c", the header file is included.

#include "foo.h"

Are there two independent foo's in two places or there is only one foo shared across "a.c" and "b.c"? I see this happens in a shared piece of code from others. Thanks in advance if you can understand this.

like image 270
sinoTrinity Avatar asked Aug 14 '26 00:08

sinoTrinity


1 Answers

You will get an error by defining an external object multiple times.

Use extern int foo; in the header file and int foo; in exactly one .c file.

The extern specifier (if there is no initializer) at file-scope declares without defining.

like image 198
ouah Avatar answered Aug 15 '26 15:08

ouah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!