我正在尝试使用find_packageCMake 中包含库。
这个问题讨论如何告诉CMake链接到GMP库(外部)。我正在尝试按照那里的答案的步骤进行操作,但没有任何<name>Config.cmake或<name>-config.cmake文件,正如一些评论中提到的,这似乎是默认的。当您不知道如何获取/查找这些文件时,答案没有提及任何解决方案。该答案的评论链接到旧网站(外部)其中有很多损坏的链接,该网站描述了加载模块列表。我不清楚这些模块从哪里来以及如何获取它们。
根据官方 CMake文档(外部),如果找不到配置文件,find_package则从“模块模式”回退到“配置模式”。我不明白这意味着什么以及在什么情况下这会相关,特别是因为文档不鼓励阅读有关“配置模式”的内容。
文档说
首先在 CMAKE_MODULE_PATH 中搜索该文件,然后在 CMake 安装提供的“查找模块”中搜索。
我仍然对这些配置文件是否应该与 CMake 一起提供还是与相关库一起提供以及它们应该位于何处感到困惑。也许两者都有可能,但在特定情况下如何知道呢?
示例代码,尝试遵循现代最佳实践:
# CMakeLists.txt (not working)
cmake_minimum_required(VERSION 3.2) # I have no idea what version I actually need
project (GMP_demo_project)
# Enable C++17 standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(GMP REQUIRED)
# Create the executable from sources
add_executable(GMP_demo GMP_demo.cpp)
target_link_libraries(GMP_demo gmp gmpxx)
Run Code Online (Sandbox Code Playgroud)
该代码输出一条错误消息,大致如下
CMake Error at CMakeLists.txt:10 (find_package):
By not providing "FindGMP.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "GMP", but
CMake did not find one.
Could not find a package configuration file provided by "GMP" with any of
the following names:
GMPConfig.cmake
gmp-config.cmake
Add the installation prefix of "GMP" to CMAKE_PREFIX_PATH or set "GMP_DIR"
to a directory containing one of the above files. If "GMP" provides a
separate development package or SDK, be sure it has been installed.
Run Code Online (Sandbox Code Playgroud)
问题:一般来说,如何获取和组织这些配置文件(CMake 加载模块)?人们怎么能期望另一用户在他的系统上拥有这些文件呢?我的问题是一般性的,仅使用 GMP 作为示例(尽管我实际上对能够使用它感兴趣)。
顺便说一句,gcc GMP_demo.cpp -lstdc++ -lgmp在按照库文档的建议安装 GMP 后,我可以编译、链接和执行我的演示代码。问题只是让 CMake 来做这件事。我也可以只给 CMake 库的绝对路径,这当然会容易得多,但不可移植(假设可以让 find_package 实际工作并通过合理的工作量进行移植)。
How does one, in general, obtain and organize these configuration files (CMake Load Modules)?
Broadly speaking, there are three buckets these fall into:
GMPConfig.cmake which cmake could find by searching preconfigured paths, or by providing a specific path at configuration time (cmake -DGMP_Dir=/path/to/GMP/install/root). The advantages of this approach are that generation of GMPConfig.cmake is mostly automatic, and the libraries can include things like installation paths and compilation flags. The disadvantage is that the library develops have to actually go to the effort of leveraging modern CMake, and not everybody does this.CMAKE_MODULE_PATH适当更新。人们怎么能期望另一用户在他的系统上拥有这些文件呢?
您的工作是安装包所需的任何依赖项。任何使用现代 CMake(上面列出的第 1 条)的内容都应在安装过程中安装 XXXConfig.cmake 文件。如果一个库是由 CMake 之外的其他东西构建的,那么您必须要么希望得到第 2 号子弹,要么找到/编写您自己的 FindXXX.cmake 文件(第 3 号子弹)。
对于您的具体情况,您可能最好使用find_library,因为您的示例编译行看起来只需要链接。
| 归档时间: |
|
| 查看次数: |
9165 次 |
| 最近记录: |