How to limit the API to be published in the C language shared library of Linux

** Trouble with API published in shared library **

I've been having a lot of trouble creating shared libraries. As the scale of the library grows, I think that naturally the inside will be divided into files and the API will be defined in the library for development.

At this time, I was always wondering if there was any way.

If you do it normally, it will be like this. ** The header API defined in the library is also included in the APIs available in the created shared library **

The public header is specified, but if you forcibly read the header used internally, that API will also be used. Especially when publishing a library on github, it's a bit crap to be able to use unintended APIs as you like.

So I checked it without expecting it.

** You can limit the API published in the shared library! ** **

There's nothing you can't do, yes with GNU! So, it seems that you can restrict publication with existing Linux functions.

Reference: http://0xcc.net/blog/archives/000108.html

For example, suppose you have a library public header like this: (Previously created time measurement library)

timetestlog.h


void * timetestlog_init(char *delimiter, size_t maxloglen, unsigned long maxstoresize);
int timetestlog_store_printf(void * handle, const char *format, ...);
void timetestlog_exit(void * handle);

As a library creator, I want you to use only this API.

Suppose you used this API when developing this library.

testfile.h


int testfunction(void);

** Normal compilation **

If you compile normally with this, you can use testfunction if you include testfile.h.

You can check the published API with the nm -D command or ʻobjdump -t`. Looking at the command execution result, you can see that the test function is included. Besides that, there are extra APIs here and there

Linux env$ nm -D .libs/libtimelog.so | grep " T "
0000000000000dec T _fini
00000000000007d8 T _init
0000000000000dc0 T testfunction
0000000000000d90 T timetestlog_exit
0000000000000b10 T timetestlog_init
0000000000000c90 T timetestlog_store_printf
Linux env$ objdump -t .libs/libtimelog.so | grep " g"
0000000000000dc0 g     F .text  000000000000002c              testfunction
0000000000202068 g       .data  0000000000000000              _edata
0000000000000dec g     F .fini  0000000000000000              _fini
0000000000000c90 g     F .text  00000000000000fc              timetestlog_store_printf
0000000000000b10 g     F .text  000000000000017f              timetestlog_init
0000000000202070 g       .bss   0000000000000000              _end
0000000000202068 g       .bss   0000000000000000              __bss_start
0000000000000d90 g     F .text  0000000000000022              timetestlog_exit
00000000000007d8 g     F .init  0000000000000000              _init

**--API restrictions by version-script **

Use "--version-script" to limit the public API. Specify the configuration file (libtimelog.map in this case) that defines version-script and public API in this way.

LDFLAGS+=-Wl,--version-script,libtimelog.map

In the configuration file, specify the API you want to expose to global, and specify other (*) in local.

libtimelog.map


{
  global:
    timetestlog_init;
    timetestlog_store_printf;
    timetestlog_exit;
  local: *;
};

Here is the public API of the library built by specifying the above configuration file. You can see that only the minimal API is exposed.

Linux env$ nm -D .libs/libtimelog.so | grep " T "
0000000000000cb0 T timetestlog_exit
0000000000000a30 T timetestlog_init
0000000000000bb0 T timetestlog_store_printf

Linux env$ objdump -t .libs/libtimelog.so | grep " g"
0000000000000bb0 g     F .text  00000000000000fc              timetestlog_store_printf
0000000000000a30 g     F .text  000000000000017f              timetestlog_init
0000000000000cb0 g     F .text  0000000000000022              timetestlog_exit

** Summary **

You can specify the public API by using ** --version-script **. Until now, I was concerned about the API to be published, and I actually put together the files I wanted to separate, but With this, you can create a library with your favorite file structure without hesitation.

Recommended Posts

How to limit the API to be published in the C language shared library of Linux
How to use the C library in Python
How to get rid of the "Tags must be an array of hashes." Error in the qiita api
From the introduction of GoogleCloudPlatform Natural Language API to how to use it
Guidelines for reincarnating in the world of linux programming development (C / C ++ language)
Hook to Shared Library on Linux to interrupt the behavior of existing binaries
[C language] How to use the crypt function on Linux [Password hashing]
How to multi-process exclusive control in C language
[C language] Be careful of the combination of buffering API and non-buffering system call
[C language] [Linux] Get the value of environment variable
linux / c> link> Get the execution result of the shell command in the C program> I was taught how to use popen ()
How to find the optimal number of clusters in k-means
How to debug the Python standard library in Visual Studio
[Linux] [C / C ++] Summary of how to get pid, ppid, tid
I tried to illustrate the time and time in C language
An introduction to the modern socket API to learn in C
Notes on how to use marshmallow in the schema library
[Linux] [C / C ++] How to get the return address value of a function and the function name of the caller
How to display a specified column of files in Linux (awk)
How to handle multiple versions of CUDA in the same environment
How to determine the existence of a selenium element in Python
Introduction to Socket API Learned in C Language Part 1 Server Edition
How to know the internal structure of an object in Python
How to change the color of just the button pressed in Tkinter
How to check the memory size of a variable in Python
How to use Python Kivy (reference) -I translated Kivy Language of API reference-
How to check the memory size of a dictionary in Python
How to output the output result of the Linux man command to a file
How to get the vertex coordinates of a feature in ArcPy
How to wrap C in Python
[Beginner memo] How to specify the library reading path in Python
I tried to display the analysis result of the natural language processing library GiNZA in an easy-to-understand manner
A story about trying to improve the testing process of a system written in C language for 20 years
Approach commentary for beginners to be in the top 1.5% (0.83732) of Kaggle Titanic_3
[C language] I want to generate random numbers in the specified range
[Linux] Command to get a list of commands executed in the past
How to compare if the contents of the objects in scipy.sparse.csr_matrix are the same
Approach commentary for beginners to be in the top 1.5% (0.83732) of Kaggle Titanic_1
What is a C language library? What is the information that is open to the public?
How to make a request to bitFlyer Lightning's Private API in Go language
Approach commentary for beginners to be in the top 1.5% (0.83732) of Kaggle Titanic_2
[Understanding in 3 minutes] The beginning of Linux
How to check the version of Django
Implement part of the process in C ++
How to use Google Test in C
[Linux] How to use the echo command
How to use the Linux grep command
How to operate Linux from the console
How to pass the execution result of a shell command in a list in Python
How to pass the path to the library built with pyenv and virtualenv in PyCharm
How to mention a user group in slack notification, how to check the id of the user group
You will be an engineer in 100 days --Day 29 --Python --Basics of the Python language 5
How to uniquely identify the source of access in the Django Generic Class View
You will be an engineer in 100 days --Day 26 --Python --Basics of the Python language 3
How to count the number of elements in Django and output to a template
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
How to find the coefficient of the trendline that passes through the vertices in Python
You will be an engineer in 100 days --Day 32 --Python --Basics of the Python language 7
How to change the appearance of unselected Foreign Key fields in Django's ModelForm
How to make the font width of jupyter notebook put in pyenv equal width
You will be an engineer in 100 days --Day 28 --Python --Basics of the Python language 4