[CMake] Best practices/preferred methods for linking external libraries

Matthew LeRoy MLeRoy at minitab.com
Thu Dec 1 16:56:26 EST 2011


We began using CMake a few months ago for a couple of small cross-platform projects, and we're still
learning all the ins and outs and capabilities CMake has to offer, as well as how to get the most
out of CMake by using it "The Right Way". Right now, we're trying to figure out how to link
to external libraries that don't have "find" modules or CMake config-mode files. After lots of
reading in the wiki, mailing list, etc, it seems like there are several different ways to do
this, like writing "find" modules and/or config-mode files ourselves, using the LINK_DIRECTORIES()
command, importing/exporting targets, and others. What we're unsure of is, what is the "preferred"
or "officially supported" method?

To be a little more specific, we have two different library projects (call them ProjectA and
ProjectB) that both use CMake, but are developed independently. Further, ProjectB depends (or will
depend) on ProjectA; we've just recently gotten to the point on ProjectB where we want to use some
of the functionality in ProjectA, so we need to link to the ProjectA library(ies). At first we
thought we needed to write a "find" module for ProjectA -- but we really have very little idea how
to go about doing that. Other than the wiki page for finding libraries that talks a little about
writing "find" modules (http://www.vtk.org/Wiki/CMake:How_To_Find_Libraries), is there any other
documentation for writing a proper "find" module? Is there a particular module that ships with CMake
that is well-written that we should look at as a guide?

Then we came across the wiki page for module maintainers
(http://www.itk.org/Wiki/CMake:Module_Maintainers), which states in the Introduction that we
should *not* write find modules for packages that themselves build with CMake, and instead to create
a CMake package configuration file. We followed the link to the tutorials on CMake Packages, but
don't fully understand everything that's going on there. Regardless, is this the preferred method?

Another general question we have is that we may not want to have to build and install ProjectA in
order to build ProjectB; we may instead just want to provide pre-built binaries and headers for
ProjectA in ProjectB's source tree in version control -- part of the reason for this is to control
which version of ProjectA is used by ProjectB. In that case, how would a find module or
package configuration file find the binaries and headers, since they're not installed in the
standard install location on the system? Further, regarding "standard install locations": is there a
particular standard location to install packages on Windows systems, similar to /usr/local (or
whatever install prefix is specified) on a Unix/Linux system?

Thanks in advance for whatever direction/guidance you can give!


More information about the CMake mailing list