Need help trying to make Cmake find third party li

2019-02-12 13:03发布

问题:

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.

回答1:

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.



回答2:

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