简体   繁体   English

需要帮助尝试使 Cmake 找到第三方库

[英]Need help trying to make Cmake find third party libraries

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.一个是taglib,另一个是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.我注意到,当您在 taglib 目录上使用 Cmake-gui 程序时,您需要指定 zlib 的构建和安装位置。

My goal is to get CMake to do a similar thing for my program.我的目标是让 CMake 为我的程序做类似的事情。 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.我希望这足够具体。

In the case of ZLib, a FindZLIB.cmake is provided with CMake and you can "simply" put a find_package call in your cmakelists.对于 ZLib,FindZLIB.cmake 与 CMake 一起提供,您可以“简单地”在您的 cmakelists 中放置一个find_package调用。 If necessary you can make some modifications to findzlib.cmake to suit your needs.如有必要,您可以对 findzlib.cmake 进行一些修改以满足您的需要。 Eg adding ZLIB_DIR as an additional hint when searching for the library.例如,在搜索库时添加 ZLIB_DIR 作为附加提示。 This ZLIB_DIR can then be set by the user.这个 ZLIB_DIR 然后可以由用户设置。

Assuming your library/executable is called YourProject, you can use it as follows.假设您的库/可执行文件名为 YourProject,您可以按如下方式使用它。

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).您应该对 TagLib 使用相同的方法,但应该编写自己的 FindTagLib.cmake(或搜索一个好的方法)。

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.这里重要的部分是您为用户提供了设置 TagLib_DIR 变量的选项,您可以使用该变量来搜索 TagLib,并使用FindPackageHandleStandardArgs来报告成功或失败。

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.为 cmake 提供自定义 zlib 或 taglib 位置。

Don't forget to update FindZLIB.cmake to handle this variables in FIND_PATH and FIND_LIBRARY不要忘记更新 FindZLIB.cmake 以处理 FIND_PATH 和 FIND_LIBRARY 中的这些变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM