Need help trying to make Cmake find third party libraries

百般思念 提交于 2019-11-30 01:41:47

问题


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



来源:https://stackoverflow.com/questions/6173915/need-help-trying-to-make-cmake-find-third-party-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!