Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

c++ - How to create a linux shared library including all binary dependencies into a single so file?

I'm able to create a shared library in linux libMySharedLibrary.so.

But I'm including a bunch of header files referencing other libraries when I compile. I want to link all the binary code from these included header files so I can distribute my shared library in one .so file.

I'm using gcc to compile. Below the command line:

gcc -I$JAVA_8_HOME/include/ -I$JAVA_8_HOME/include/linux/ -I./include/ -I. -fPIC -o libMyLibrary.so -shared com__MyLibrary.c

The header files are inside ./include. I need to link the generated libMyLibrary.so with the binaries of the header files inside ./include.

How?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I want to link all the binary code from these included header files so I can distribute my shared library in one .so file.

You cannot do what you want (and that is against the whole idea of shared libraries, which, as their name imply, are shared between several processes and "loaded" into their virtual address space by the dynamic linker).

You should read Drepper's How To Write Shared Libraries paper and the Program Library HowTo. See also ld-linux(8) & elf(5) & objdump(1) & readelf(1) & ld(1) & ldd(1). Read about Invoking GCC.

To ease distribution of your software, you may want to build some package (e.g. a .deb one) targetting some package manager. And you might publish your source code as free software (e.g. on github).

You could (and perhaps should) link other lower-level shared libraries with your own one. But your user would still need to have these libraries installed. For example, you might compile with

 gcc -Wall -O -g -I$JAVA_8_HOME/include/ -I$JAVA_8_HOME/include/linux/ 
     -I./include/ -I. -fPIC -o libMyLibrary.so 
     -shared com__MyLibrary.c -lother

and check with ldd libMyLibrary.so that it depends on some libother.so.* (which your user should have installed to use your libMyLibrary.so).

You could compile all the code you are using (as position-independent code) -including the source code of "other libraries"- into a single shared library, but that is not recommended (if you did that, a program could practicaly not use some shared low-level function in several shared libraries above it).

You also need to understand that a library is not a set of header files, that is to understand the roles of the linker and of the preprocessor. Header files just describe (partly) the API of some C or C++ library. Read also Levine's Linkers and Loaders book. In practice, header files declare some stuff (classes, functions, variables, types...) and might define inline functions (but not global ones). You need to understand what translation units are.

You could, with the help of the /proc/ file system (see proc(5)), understand the virtual address space of some given process. For instance, try cat /proc/$$/maps in a terminal.

You may read some Linux programming book, perhaps the old ALP (freely downloadable) or something newer. You may read some textbook on operating systems, such as Operating Systems: Three Easy Pieces (also freely downloadable).

Look also for inspiration into the source code and build procedure of existing free software libraries (e.g. from your Linux distribution, or on github or elsewhere). You should consider using some build automation tool (e.g. GNU make, or ninja) to build your own library.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...