How to get CMake find what you want it to

Since some August or so we are now requiring CMake 2.6 for KDE svn trunk.

One thing was has been added and which is very nice support for the new environment variable CMAKE_PREFIX_PATH. It's purpose is to help with getting CMake to find what you want it to find.

Usually a software package is installed to some "prefix" directory, in the lib/ subdir of that prefix go the libraries, in the include/ subdir of that prefix go the headers, and in the bin/ subdir of that prefix go the programs.

By default, if you use one of the find_(file|path|library|program)() commands, CMake searches in a set of such prefix directories, e.g. in /usr, /usr/local, /opt/csw and more (i.e. in the include/, library/ and bin/ directories depending on whether find_(file|path|library|program) was called. Have a look at Modules/Platform/UnixPath.cmake for the full set of default search directories.

Since CMake 2.6 it also searches in CMAKE_INSTALL_PREFIX of the current project and in the directory where CMake itself is installed.
This works successfully in a lot of case.

But let's say, you have some custom install directories on your system, e.g. if you are a kdepim developer you might have kdesupport and kdelibs in /opt/kde4, while you install kdepimlibs and kdepim to $HOME/kdepim/.

Or, as another example, I have kdesupport in /opt/kdesupport, kdelibs in /opt/kdelibs, kdepimlibs in /opt/kdepimlibs, qt-copy in /opt/qt-copy, the rest of KDE4 in /opt/kde4.

What's the best way to get CMake 2.6 find all that ?
By setting the environment variable CMAKE_PREFIX_PATH, which is a list of directories (similar to PATH). The prefixes (directories) listed in it will be searched before the default search directories, and lib/, include/ and bin/ will be appended appropriately.

So, this is what I do to get cmake find all my KDE4 stuff:

$ export CMAKE_PREFIX_PATH=/opt/qt-copy:/opt/kdesupport:/opt/kdelibs:/opt/kdepimlibs:/opt/kde4

Did you notice that I also put the directory of qt-copy there ? This also makes sure that CMake finds the qmake sitting there first, and I don't have to change the PATH additionally.

Now what would the aforementioned kdepim developer do ?
I guess something like:

$ export CMAKE_PREFIX_PATH=$HOME/kdepim:/opt/kde4

This environment variable has to be set (only) at CMake-time. And once CMake has found the stuff, it shouldn't be required anymore, authors of FindFoo.cmake modules should take care of that (i.e. that everything is properly stored in the cache).

So, what to keep from that ?

If you have some custom install directories on your system and you want CMake to find the stuff installed there, set CMAKE_PREFIX_PATH.


Now what's a good way to get CMake t


I attempt to compile kdebase from trunk against kdelibs and kdesupport in ~/kde. So following your instruction i set CMAKE_PREFIX_PATH to $HOME/kde. But cmake keeps finding nepomuk and soprano in /usr, making the build break.

By sredna at Sat, 12/20/2008 - 17:13

It works. First, export the environment variable. Second, delete the build directory if exists. Last, build and enjoy:)

By KDE User at Sat, 03/19/2016 - 03:21

I've been trying many variants of this command, and at last, afer deleting the build directory, it's working great

By OSX_avr_user at Sun, 02/12/2017 - 17:15

Almost 8 years later and still google pointed me here and I solved my problem.

By hashier at Thu, 05/19/2016 - 10:05

Thanks. This helped.

By Kiran at Thu, 06/23/2016 - 08:49

I just want to say thank you for your post.

By Dang at Fri, 07/27/2018 - 03:02