CMake:How To Find Installed Software: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
No edit summary
No edit summary
Line 2: Line 2:
Depending on the location appropriate include directories and library search paths will have to be added to the compile commands.
Depending on the location appropriate include directories and library search paths will have to be added to the compile commands.


CMake helps you with this by providing so-called modules. Let's say you want to use the PNG-Library.
CMake helps you with this by providing so-called modules. Let's say you want to use the PNG-Library, here's how you would do this with cmake:
<pre>
<pre>
FIND_PACKAGE(PNG)
FIND_PACKAGE(PNG)
Line 20: Line 20:
* <name>_INCLUDE_DIR or <name>_INCLUDES
* <name>_INCLUDE_DIR or <name>_INCLUDES
* <name>_LIBRARY or <name>_LIBRARIES
* <name>_LIBRARY or <name>_LIBRARIES
As you can see, with PNG_FOUND you can test whether the package you need has actually been found. If that's the case, you have to add the include directories and link to the libraries which make up the package, in this case PNG_INCLUDE_DIR and PNG_LIBRARIES.
If the package is not optional but your software can't be built at all without the package, you should use the REQUIRED argument for FIND_PACKAGE(). This will have the effect that cmake will abort with an error if it can't find the software.
But where does cmake actually look for the package, i.e. for the headers and libraries ?
We'll go into details in the next section.


{{CMake/Template/Footer}}
{{CMake/Template/Footer}}

Revision as of 20:51, 12 February 2006

If your software uses external libraries (i.e. libraries not coming with your software), you don't know in advance where its headers and libraries will be located on the system where your software will be compiled. Depending on the location appropriate include directories and library search paths will have to be added to the compile commands.

CMake helps you with this by providing so-called modules. Let's say you want to use the PNG-Library, here's how you would do this with cmake:

FIND_PACKAGE(PNG)

IF(PNG_FOUND)
   INCLUDE_DIRECTORIES(${PNG_INCLUDE_DIR})

   ADD_EXECUTABLE(imageviewer main.c image.c)
   TARGET_LINK_LIBRARIES(imageviewer ${PNG_LIBRARY})

ENDIF(PNG_FOUND)

Every module is provided in the form Find<name>.cmake, they are located in the CMake module directory, on UNIX usualy /usr/local/share/CMake/Modules/ It is then used in the CMakeLists.txt with the FIND_PACKAGE(<name>) command. For details see the regular CMake documentation. Every module will define the following variables:

  • <name>_FOUND
  • <name>_INCLUDE_DIR or <name>_INCLUDES
  • <name>_LIBRARY or <name>_LIBRARIES

As you can see, with PNG_FOUND you can test whether the package you need has actually been found. If that's the case, you have to add the include directories and link to the libraries which make up the package, in this case PNG_INCLUDE_DIR and PNG_LIBRARIES.

If the package is not optional but your software can't be built at all without the package, you should use the REQUIRED argument for FIND_PACKAGE(). This will have the effect that cmake will abort with an error if it can't find the software. But where does cmake actually look for the package, i.e. for the headers and libraries ?

We'll go into details in the next section.



CMake: [Welcome | Site Map]