How to specify which function to export from a .so library when compiling C code?

I have a number of functions in the "C" code. When I compile .so, I see all the names in the .so file. Result. How can I indicate (in the code or in the make file) that only some functions should be exported, and the rest are for internal use only.

+4
source share
2 answers

In C, if you want the function to remain internal to the file (technically, the โ€œcompilation unitโ€) that contains it, you declare it โ€œstaticโ€. For instance,

static int privateAddOne(int x) { return x + 1; } 
+7
source

Since you mention .so files, it seems like a reasonable assumption that you are using gcc or the gcc-like compiler.

By default, all extern functions are visible in the associated object. You can hide functions (and global variables) in each case, using the hidden attribute (while preserving extern , which allows you to use them from other source files in the same library):

 int __attribute__((visibility("hidden"))) foo(void) { return 10; } 

Alternatively, you can change the default value to hidden by passing the -fvisibility=hidden parameter to gcc at compile time. Then you can mark certain functions for export using:

 __attribute__((visibility("default"))) 
+18
source

Source: https://habr.com/ru/post/1332433/


All Articles