What Are Good Practices Regarding Shared Libraries on Linux

What are good practices regarding shared libraries on Linux?

Suggested reading:

Ulrich Drepper's How to write shared libraries: http://www.akkadia.org/drepper/dsohowto.pdf

Ulrich Drepper's Good Practices in library design, implementation, and maintenance: http://www.akkadia.org/drepper/goodpractice.pdf

dsohowto is much more detailed. goodpractice is a quick read.

C portable shared library interface: best practice on primitive types

Typically all compilers for a given platform try very hard to preserve the default C ABI. Violation of ABI is normally considered a compiler bug.

C++ ABI is trickier for various reasons but at least Clang tries hard to preserve that one on Windows as well.

C ABI compatibility means, among other things, that all primitive types have defined size and alignment so there is no need to use fixed-width types (i.e. long will be the same for all compilers for a particular target).

As for your case, I suspect that clang and cl.exe are using different time.h for whatever reason so I suggest to look into that (see my comment above on how to proceed with this).

best practices on my library coded in C

I would recommend putting the files somewhere in your $HOME directory. Let's say you've created a library called linluk and you want to keep that library under $HOME/dev/c/linluk. That would be your project root.

You'll want a sensible directory structure. My suggestion is to have a lib directory containing your library and an include directory with the header files.

$PROJECT_ROOT/
lib/
liblinluk.so
include/
linluk.h
src/
linluk.c
Makefile

Compiling: When you want to use this library in another project, you'd then add -I$PROJECT_ROOT/include to the compile line so that you could write #include <linluk.h> in the source files.

Linking: You would add -L$PROJECT_ROOT/lib -llinluk to the linker command line to pull in the compiled library at link time. Make sure your .so file has that lib prefix: it should be liblinluk.so, not linluk.so.

A src directory and Makefile are optional. The source code wouldn't be needed by users of the library, though it might be helpful to have it there in case someone wants to remake the .so file.

Version control best practices for libraries with SONAME

Since a soname describes the version of a library's interface, rather than its implementation, it corresponds roughly to a "release" in terms of software lifecycle management. In most version control systems (including Perforce), that means you want to represent it as some form of branch (each of which may have an arbitrary number of revisions corresponding to implementation changes) rather than mapping each individual revision/commit to its own soname.

Perforce's inter-file branching system makes this very easy to model -- when it's time to rev the soname, you branch the file to its new name, just like you'd naturally use cp if you weren't using version control. (Unlike git, you don't have to branch the entire repo all as a unit; each individual file can have its own branching structure if you want, and the variants are represented within the client filesystem as well as in the repo.)

If your current project needs a particular soname variant and you don't want to pull down all the others when you sync, set up your client view to only map the one you want.

What is the best practice for a shared library primary header file in C++?

There is nothing in the standard governing "allowed", "prohibited" or "best practices" regarding extensions for filenames.

Use whichever form you prefer. On some platforms there's a convenience factor to having an file extensions for registered types.

For what it's worth <string.h> and <string> are totally different headers. The C++ namespaced equivalent of <string.h> is actually <cstring>.

Managment/Filenames of shared libraries

The reason this is done is so a program can depend on as specific a version of a library as it wants.

For example, a program may say "I need libs". The current default version of libz.so would point at libz.so.1.2.5.

Another program may say "I need version 1 of libz". The current default version of libz.so.1 points at libz.so.1.2.5.

libz.so.1.2.5 mostly exists so you know exactly what version you have installed. You can have multiple versions, and switch the symlinks around as necessary, but this is generally not done.

Best practice to create Shared library packed with big data

I don't think there is one "right" answer to this.

Storing data in the file is fine, as long as the data isn't changing more often than you wish to release a new library - you need the amount of storage in some way or another anyway, so as long as the compiler doesn't do a terrible job with storing the data in the shared library, it's no worse than any other options, as far as I see it.

Having a secondary file is only useful if you expect the data to be changed more often than you wish to release a new shared library. It adds the extra complication of opening and reading the secondary file - the drawback is that you then also need to add checking that it's correct/present and code dealing with it not being there.

If you do have a secondary file, having SOME way to redefine the location would definitely be beneficial.

If the data is really large, you may want to use a compressed format. You can still store compressed data as data in your shared library, and use a compression library that can expand the data from that. Or you can use a library that reads from an external file...

In the end, it really comes down to:

  1. How you are using the data - do you always need ALL of it, or do you just need some of it at times? If the latter, how do you know which bits?
  2. How often the data changes.
  3. If the data can be compressed or not, and if so by what method do you compress it?

I'm not sure there is any direct size limits on a shared library - if you need 1GB of data, then you need 1GB of space in memory either way, so it's not like you are saving memory [assuming you always need ALL the data and/or can't determine which parts you need].



Related Topics



Leave a reply



Submit