I have a number of function in my "C" code. When I compile .so, I see all names in result .so file. How can I specify (in code or in make file) that only some functions should be exported, where as others are private just for internal use.
Since you mention .so
files, it seems like a reasonable assumption that you're using gcc
or a gcc-alike compiler.
By default all extern
functions are visible in the linked object. You can hide functions (and global variables) on a case-by-case basis using the hidden
attribute (while keeping them extern
, which allows them to be used from other source files in the same library):
int __attribute__((visibility("hidden"))) foo(void)
{
return 10;
}
Alternatively you can change the default to hidden
by passing the -fvisibility=hidden
option to gcc
at compile time. You can then mark particular functions for export using:
__attribute__((visibility("default")))
In C, if you want a function to remain internal to the file (technically, the "compilation unit") that contains it, you declare it "static". For example,
static int privateAddOne(int x) { return x + 1; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With