Dependency Management
This page is under construction. Please come back later.
It is often easier to write programs when you don't have to write all the code from scratch. There are a large number of code libraries available, both open source (free) and commercial (paid). Using code libraries written by the community or commercial developers give you a leg up in your coding efforts by making a large library of useful functions and classes available to your program. All we need to do is manage these dependencies and include them in our programs.
C++ doesn't come with a package manager. Typically you can download pre-compiled C++ libraries from the website for that library. You then put these library files in your project folder and link them to your program.
- If you are on a Unix-like system, you can generally use
apt
to download libraries and install them in the system library folder. - On MacOS you can try to install libraries using a Unix package manager like [[MacPorts]] or [[Homebrew]].
- On Windows you can try to install libraries using the [[Cygwin]] setup program.
Once your library is installed, you simply add the -l
option when you are compiling your program to link to the library. For example to link to the fmt
library use:
If your library is implemented using templates as a pure header file, all you need to do is download the .hpp
header file and place it in the same folder as your program source. This is the way the Pure Programmer utils library is implemented. Simply download the [[Pure Programmer C++ Utility Templates]].
It doesn't matter whether you installed a precompiled library or are using a pure header library, the way you use the library is the same. In your .cpp
source file, simply put an include
preprocessor directive at the beginning of your file. For example, to include the Pure Programmer library downloaded above simply add an include for "Utils.hpp":
For libraries that reside in your project directory, you must enclose the header file name in double quotes. For libraries that come with C++ or are installed in your system folders, use the angle brackets instead.
References
-
[[C++ Programming Language]], 4th Edition, Bjarne Stroustrup, Addison-Wesley, 2013, ISBN 978-0321563842.
- [[C++ Language Reference]]
- [[cplusplus.com]]
- [[Cprogramming.com]]