Path to store libraries. Best practice needed

2.6k views Asked by At

I'm planing to make several projects in C and C++ under Linux. In some projects I will need additional libraries. Correct me if I'm wrong. I'm planning to store libs in /usr/local/include. But is it good practice to mix all libs in one directory? Maybe it is better to create libs name subdirectories in /usr/local/include directory? And maybe it is better to store headers and source in different directories? Best practice is needed.

How to deal with not compiled libs like for example MiniIni https://code.google.com/p/minini/. It comes with header and C files. Should I compile it and place in /lib directory and headers to /include. Or maybe it is better place everything in /include?

4

There are 4 answers

2
pmr On

If you talk about libs I assume you mean compiled libraries (.so files or .a files). Those should go into /usr/local/lib/. Headers should go into /usr/local/include. Sources .cpp files usually should not go anywhere in the installation. Sometimes it is necessary to install them, so they can be rebuild on demand (dkms comes to mind). Then the sources should go into /usr/local/src/project_name/.

I personally prefer headers and libs to be installed in sub-directories of /usr/local/include and /usr/local/lib, but not everybody will agree.

The /usr/local prefix always should be configurable. While a traditional make install should use it as a default, packagers on distros will certainly change it to install directly into /usr.

0
Guntram Blohm On

I hope you aren't storing libs in /usr/local/include. They belong in /usr/local/lib or possibly /usr/local/lib64.

Headers needed for individual projects should NOT go into /usr/local/include, they should stay with the project. Only put stuff into /usr/local/lib and /usr/local/include if you are building a library to be included in several of your projects. Even then, i'd keep the headers local to the project, and only copy them to /usr/local/include as part of the build process.

As to same directory or different within your project tree, it depends on how big the project is. I typically start moving stuff into seperate directories when the number of files (source + include) starts exceeding 20.

0
exAres On

Now coming to your question of using subdirectories. It is never a good practice to mix all libraries in a single directory. So better way is to have subdirectories and then referring the respective files using "relative" paths.

0
sancelot On

I would suggest to store all your library in a customlib directory.

set the LD_LIBRARY_PATH environment variable to this directory or use -L option if using gcc (eg for minini).

like this your work is non intrusive in your syste m, and will not break it.