[CMake] How to properly write a Module/<Package>Config.cmake file

Andreas Pakulat apaku at gmx.de
Mon Nov 24 11:54:57 EST 2008

On 24.11.08 17:00:43, Pablo Yanez Trujillo wrote:
> Hi
> I'm reading http://www.cmake.org/cmake/help/cmake2.6docs.html#command:find_package for the usage of FIND_PACKAGE, but
> I'm not interested in using the FIND_PACKAGE command but to write a Module so that anybody is able to use
> FIND_PACKAGE(myLib ...).
> I don't really unterstand what exactly is done by FIND_PACKAGE. The docu states that FIND_PACKAGES searches for
> FindmyLib.cmake and executes it. If the package is found then the variables myLib_FOUND is set. But who sets this
> variable? FIND_PACKAGE or FindmyLib.cmake? And how does FIND_PACKAGE know that a package was not found?
> My second question is:
> the simple signature is of FIND_PACKAGE is
> find_package(<package> [version] [EXACT] [QUIET]
>                [[REQUIRED|COMPONENTS] [components...]])
> How does FindmyLib.cmake get the version argument of the FIND_PACKAGE call?
> What is really the difference between FindmyLib.cmake and myLibConfig.cmake? When should I write the first one and when
> the second one? Or should I always write both?

You should favor a FooConfig.cmake. If you read all the paragraphs in man
cmake about find_package you'll see that CMake will set a couple of
variables for you, for example 3 variables for the version number that has
been requested. Your FooConfig.cmake is then expected to set a bunch of
variables as well and from that CMake will know wether finding the library
was successful or not.

One can also do both, that way you can do some custom things in your
FindFoo.cmake and then just call find_package(Foo NO_MODULE) to let
FooConfig.cmake do the rest. This can be useful if FooConfig.cmake is in a
strange location, or you want to do other pre-processing.

For an example of all this you can have a look at:

The CMakeLists.txt generating the real files from the cmake.in version is

And the CMakeLists.txt using the FindKDevPlatform.cmake to find the
kdevplatform module is here:


Keep it short for pithy sake.

More information about the CMake mailing list