Reputation: 776
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:
include <header.h>
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
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