Find as External Package
If you have the library installed on your system, you can call find_package()
from your CMake build script.
1 |
|
When creating your executable, link the library to the targets you want:
1 2 |
|
Then add this header to your source files:
1 |
|
You can see a complete example in test/integration/CMakeLists.txt
.
CMake should be able to locate the Matplot++Config.cmake
script automatically if you installed the library under /usr/local/
(Linux / Mac OS). Unfortunately, there is no easy default directory for find_package
on Windows.
Default directories
By default, the library is likely to be in /usr/local/
(Linux / Mac OS) or C:/Program Files/
(Windows). The installer will try to find the directory where you usually keep your libraries but that's not always perfect.
Finding packages on Windows
Unfortunately, CMake does not have a single default directory for packages on Windows like /usr/local/lib
. If CMake cannot find Matplot++ on Windows or if you installed the library outside the default directory on Linux/Mac OS, there are a few options:
- Environment Variables: The most reliable way to set this default directory is through environment variables. You can create an environment variable
MATPLOTPP_DIR
and then add$ENV{MATPLOTPP_DIR}
to theHINTS
section of thefind_package
command. This tends to be more convenient than requiring the path on the command line every time. Starting with version 3.12, CMake now implicitly considers the<PackageName>_Root
environment variable a HINT for everyfind_package
call. - Package Registry: CMake offers the Package Registry as an alternative mechanism for finding package locations. CMake maintains a list of package information in the Windows registry under
HKEY_CURRENT_USER\Software\Kitware\CMake\Packages\
. - Append CMAKE_MODULE_PATH: You can append more directories to
CMAKE_MODULE_PATH
with something likelist(APPEND CMAKE_MODULE_PATH "C:\\Program Files\\matplotplusplus 1.0.1")
.CMAKE_MODULE_PATH
is a list of search paths for CMake modules to be loaded by theinclude()
orfind_package()
commands. - Set the DIR variable directly: Directly set the
Matplot++_DIR
variable with something likeset(Matplot++_DIR "C:\\Program Files\\matplotplusplus 1.0.1\\lib\\cmake\\Matplot++")
. This might be good enough for small local projects but it is hard-coding the directory in your build script. When your library gets out of your local environment, you need to choose one of the other options above (better) or make this variable an option and require the user to provide the directory on the command line every time (worse).