Need help trying to make Cmake find third party libraries

Dr.McNinja picture Dr.McNinja · May 30, 2011 · Viewed 15.1k times · Source

I currently have a project that links to two third party libraries. These libraries have to be built by themselves and then linked to the project. One is taglib and the other is zlib. I noticed that when you use the Cmake-gui program on the taglib directory you're required to specify where zlib has been built and installed.

My goal is to get CMake to do a similar thing for my program. Since the place these libraries are stored will be inconsistent how can I prompt the user to provide the path to the libraries required?

I hope this is specific enough.

Answer

André picture André · May 30, 2011

In the case of ZLib, a FindZLIB.cmake is provided with CMake and you can "simply" put a find_package call in your cmakelists. If necessary you can make some modifications to findzlib.cmake to suit your needs. E.g. adding ZLIB_DIR as an additional hint when searching for the library. This ZLIB_DIR can then be set by the user.

Assuming your library/executable is called YourProject, you can use it as follows.

find_package( ZLIB REQUIRED )
if ( ZLIB_FOUND )
    include_directories( ${ZLIB_INCLUDE_DIRS} )
    target_link_libraries( YourProject ${ZLIB_LIBRARIES} )
endif( ZLIB_FOUND )

You should use the same approach for TagLib, but instead should write your own FindTagLib.cmake (or search for a good one).

The important part here is that you give the user the option to set a TagLib_DIR variable, which you use to search for TagLib and that you use FindPackageHandleStandardArgs to report success or failure.