Gianluca Bianco
Gianluca Bianco

Reputation: 776

How to add a static library permanently to the Ubuntu system?

Hi everybody I recently created a C++ project in which I put my code into header.h and header.cpp files and successfully create a static library called header.a. Now I put my header.h file into /usr/local/include position of my system and header.a into /usr/local/lib in order to "install" my library into the machine. Now, if I want to use this library, I have to do the following steps:

  1. Suppose I am using a main.cpp program, I include this line at the top of the program:
include <header.h>
  1. Then, I compile with this command:
g++ main.cpp /usr/local/lib/header.a

And all it's ok. But I want to find a way to "store" permanently the header.a library into the system in order to use it like a normal standard C++ header, simplifing the compilation in this way:

g++ main.cpp

Is there a way to do this? Many thanks to all.

Upvotes: 0

Views: 1368

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409136

You can't, and no system library will be linked automatically without being told to do so.

You can however add the path /usr/local/lib to the default paths for the linker to look for libraries (IIRC it's not in there by default for Ubuntu), which means you only need to add the -l (lower-case L) option to link with the library.

But do note that the library should be having a lib prefix. Like in libheader.a.

Then link with -lheader:

g++ main.cpp -lheader

There also the -L option to add a path to the list of paths that the linker searches, if you have other non-standard paths, of if you can't edit the system configuration to use /usr/local/lib:

g++ main.cpp -L/usr/local/lib -lheader

The library file still needs the lib prefix.

Upvotes: 2

Related Questions