12

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.

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
Dr.McNinja
  • 455
  • 1
  • 6
  • 15

2 Answers2

22

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.

SimplyKnownAsG
  • 904
  • 9
  • 26
André
  • 18,348
  • 6
  • 60
  • 74
  • So all I do is put this FindTaglib.cmake in the same directory as the CMakeLists.txt? – Dr.McNinja May 30 '11 at 19:23
  • It searches for FindTagLib.cmake in CMAKE_MODULE_PATH. I am not sure if the main dir with CMakeLists.txt is already in there. If not you can add it: set( CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ) – André May 30 '11 at 20:03
1

Not sure about interactive prompt, but you always can use environment variables or following:

cmake -D<VAR_NAME>:STRING=<path to custom zlib> .

to provide cmake with custom zlib or taglib location.

Don't forget to update FindZLIB.cmake to handle this variables in FIND_PATH and FIND_LIBRARY

Sergei Nikulov
  • 5,029
  • 23
  • 36