Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping variables global to the library scope in C

Tags:

c

scope

Is there any way to keep global variables visible only from inside a library while inaccessible from programs that access that library in C?

It's not that it is vital to keep the variable protected, but I would rather it if programs couldn't import it as it is nothing of their business.

I don't care about solutions involving macros.


1 Answers

If you use g++, you can use the linker facilities for that using attributes.

__attribute__((visibility("hidden"))) int whatever;

You can also mark everything as hidden and mark explicitly what is visible with this flag: -fvisibility=hidden

And then mark the visible variables with:

__attribute__((visibility("default"))) int whatever;
like image 85
Arkaitz Jimenez Avatar answered Dec 07 '25 15:12

Arkaitz Jimenez