Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a library?

Let's say I have 10 *.hpp and *.cpp files that I need to compile a code. I know that I will need those same files for many different codes. Can I create a "package" with those files that would allow me to simply write:

#include <mypackage> 

instead of:

#include "file1.hpp" #include "file2.hpp" ... #include "file10.hpp" 

I wouldn't then need to write a makefile every time I need this "package".

To be more precise, I use Linux.

like image 413
PinkFloyd Avatar asked May 22 '13 13:05

PinkFloyd


People also ask

How do you create a simple library?

Create the libawesome Folder and Project Click the Save Project As button and navigate to … Documents\SimpleIDE\Learn\Simple Libraries. Click the Open button to go into the My Libraries folder. Click New Folder again, and this time, name the new folder libawesome.

When should you create a library?

As soon as you tire of copying and pasting you should create a library. Or, as soon as you make your first mistake (mis-)copying and (mis-)pasting. Or, in more business-like terms: when the net present value exceeds the net present cost.


2 Answers

A collection of CPP sources (H files and CPP files) can be compiled together in to a "library," which can then be used in other programs and libraries. The specifics of how to do this are platform- and toolchain-specific, so I leave it to you to discover the details. However, I'll provide a couple links that you can have a read of:

Creating a shared and static library with the gnu compiler [gcc]

Walkthrough: Creating and Using a Dynamic Link Library (C++)

Libraries can be seperated in to two types: source code libraries, and binary libraries. There can also be hybrids of these two types -- a library can be both a source and binary library. Source code libraries are simply that: a collection of code distributed as just source code; typically header files. Most of the Boost libraries are of this type. Binary libraries are compiled in to a package that is runtime-loadable by a client program.

Even in the case of binary libraries (and obviously in the case of source libraries), a header file (or multiple header files) must be provided to the user of the library. This tells the compiler of the client program what functions etc to look for in the library. What is often done by library writers is a single, master header file is composed with declarations of everything that is exported by the library, and the client will #include that header. Later, in the case of binary libraries, the client program will "link" to the library, and this resolves all the names mentioned in the header to executable addresses.

When composing the client-side header file, keep complexity in mind. There may be many cases where some of your clients only want to use some few parts of your library. If you compose one master header file that includes everything from your library, your clients compilation times will be needlessly increased.

A common way of dealing with this problem is to provide individual header files for correlated parts of your library. If you think of all of Boost a single library, then Boost is an example of this. Boost is an enormous library, but if all you want is the regex functionality, you can only #include the regex-related header(s) to get that functionality. You don't have to include all of Boost if all you want is the regex stuff.

Under both Windows and Linux, binary libraries can be further subdivided in to two types: dynamic and static. In the case of static libraries, the code of the library is actually "imported" (for lack of a better term) in to the executable of the client program. A static library is distributed by you, but this is only needed by the client during the compilation step. This is handy when you do not want to force your client to have to distribute additional files with their program. It also helps to avoid Dependancy Hell. A Dynamic library, on the other hand, is not "imported" in to the client program directly, buy dynamically loaded by the client program when it executes. This both reduces the size of the client program and potentially the disc footprint in cases where multiple programs use the same dynamic library, but the library binary must be distributed & installed with the client program.

like image 87
John Dibling Avatar answered Oct 07 '22 06:10

John Dibling


On Linux:

g++ FLAGS -shared -Wl,-soname,libLIBNAME.so.1 -o libLIBNAME.VERSION OBJECT_FILES

where

FLAGS: typical flags (e.g., -g, -Wall, -Wextra, etc.)

LIBNAME: name of your library

OBJECT_FILES: objects files resulting from compiling cpp files

VERSION: version of your library

like image 21
Claudio Avatar answered Oct 07 '22 07:10

Claudio