How could I add a external library to the native c++ lirary path at Ubuntu to be found by #include from source code? I have some couple of sources that I try to include them by all the codes written on my machine thus instead of coping all the library every code I wrote, I plan to define it as a native c++ library that can be rached by all codes with single #include.

Recommended Answers

All 3 Replies

why not just copy the libs anywhere you want on the file system then use the -L<directory> to tell gcc where the library is located.

What AD suggests is the preferred thing, i.e., passing the libraries and include-paths manually to the command-line, or configure you projects to add your library and include-paths to them. However, you shouldn't put them "anywhere you want", either put them in your home folder or somewhere that isn't a system path (not on the root partition).

If you really want to install them on the system, here's what you need to do:

So, normally, in the Linux file system, you have the following directories for installed software and libraries:

/usr/bin : for executables (and scripts)
/usr/lib : for binary libraries (static (.a) and shared libraries (.so))
/usr/include : for header files (all languages)

So, you could "install" your library's headers and binaries in those folders and then you will be able to use them like any other system library (i.e., when you install development packages or any software or library from the repositories (apt-get or yum) that's where things are installed, with the addition of /usr/share for application data (images, system-wide configurations, etc.)).

However, it's not a good idea to put your own hand-rolled libraries and stuff in there because it will be mixed up with official system libraries, and you don't want that until your library has achieved really good maturity. There is an additional set of directories for that specific purpose:


which are just add-on folders for the top-level folders of the same name, but it is much easier to manage your own "unofficial" system libraries if they reside in those folders. And, in most build systems, those local folders take precedence over the top-level ones because often, if you build a libraries or software from source code, you put them there and those newer versions will take precedence over the installed versions (if any). So, this is pretty much where you'd want to put your own libraries. IMPORTANT: You must create a sub-folder for your library, do not put your headers directly under /usr/local/include. That should be obvious, but in case you didn't know.

What mike_2k said, but understand that for an application to use the libraries you installed in /usr/local/lib, you will need to update the LD_LIBRARY_PATH environment variable to search there. IE, in your .bash_profile add this line:

export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/usr/local/lib"

The reason for the {} around LD_LIBRARY_PATH is because you are using it recursively to redefine the LD_LIBRARY_PATH environment variable, so just using $LD_LIBRARY_PATH is not safe.

If you want libraries in /usr/local/lib to be found first, then switch the LD_LIBRARY_PATH and /usr/local/lib around, as in

export LD_LIBRARY_PATH="/usr/local/lib:${LD_LIBRARY_PATH}"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.