我应该在哪里安装 myproj-config.cmake 和 myproj-version-config.cmake?

发布于 2025-01-18 14:01:27 字数 1078 浏览 2 评论 0原文

假设您使用cmake进行构建配置来开发一些库,myproj;支持cmake -install(使用install()命令);并通过CMake Config模式支持myproj,即通过使相关.cmake文件可访问相关项目。

现在,给定一个安装根目录 - 我应该在哪里安装项目的配置.cmake文件?是否有惯用标准(ISH)位置?

  • Sorush Khajepor的R& d博客建议 myproj - 这是最新的。
  • Foonathan的博客建议放置config config .cmake coce> $ {lib_install_dir}/中的文件。 falkor's Blog
  • 文档 page 建议:$ {lib_install_dir}/myproj/cmake

最受欢迎/最惯用的选择是什么?相对于其他方面的利弊是什么?

Suppose you're developing some library, myproj, using CMake for build configuration; supporting the cmake --install (using install() commands); and supporting use of myproj with CMake config mode, i.e. by making relevant .cmake files accessible to dependent projects.

Now, ,given an install root directory - where should I install my project's configuration .cmake files? Is there an idiomatic standard(ish) location?

  • Sorush Khajepor's R&D blog suggests ${LIB_INSTALL_DIR}/cmake/myproj - and it's the newest.
  • Foonathan's blog suggests placing the config .cmake files in ${LIB_INSTALL_DIR}/. So does Falkor's blog.
  • The documentation page for the CMakePackageConfigHelpers module suggests: ${LIB_INSTALL_DIR}/myproj/cmake.

What's the most popular/idiomatic choice? And what are its pros and cons relative to the other ones?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看海 2025-01-25 14:01:27

我主张设置一个缓存变量来覆盖它,并将其默认为 /cmake/ProjName (正如您在答案中所建议的那样):

cmake_minimum_required(VERSION 3.21)  # for saner CACHE variables
project(ProjName VERSION 0.1.0)

# ...

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

set(ProjName_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/ProjName"
    CACHE STRING "Path to ProjName CMake files")

install(EXPORT ProjName_Targets
        DESTINATION "${ProjName_INSTALL_CMAKEDIR}"
        NAMESPACE ProjName::
        FILE ProjNameConfig.cmake
        COMPONENT ProjName_Development)

write_basic_package_version_file(
    ProjNameConfigVersion.cmake
    COMPATIBILITY SameMajorVersion)

install(FILES
        "${CMAKE_CURRENT_BINARY_DIR}/ProjNameConfigVersion.cmake"
        DESTINATION "${ProjName_INSTALL_CMAKEDIR}"
        COMPONENT ProjName_Development)

我写了一篇博客文章,其中包含此内容的扩展版本回来时: https://alexreinking.com/blog/building-a-dual-shared-and-static-library-with-cmake.html

一般来说,设置一个 install() 除了“${SOME_CACHE_VARIABLE}”之外的任何目标肯定会让某些软件包维护者感到头疼。如果 GNUInstallDirs 未提供有效的配置点,您必须创建自己的配置点。

I advocate for setting a cache variable to override this and defaulting it to <LIBDIR>/cmake/ProjName (as you suggest in your answer):

cmake_minimum_required(VERSION 3.21)  # for saner CACHE variables
project(ProjName VERSION 0.1.0)

# ...

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

set(ProjName_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/ProjName"
    CACHE STRING "Path to ProjName CMake files")

install(EXPORT ProjName_Targets
        DESTINATION "${ProjName_INSTALL_CMAKEDIR}"
        NAMESPACE ProjName::
        FILE ProjNameConfig.cmake
        COMPONENT ProjName_Development)

write_basic_package_version_file(
    ProjNameConfigVersion.cmake
    COMPATIBILITY SameMajorVersion)

install(FILES
        "${CMAKE_CURRENT_BINARY_DIR}/ProjNameConfigVersion.cmake"
        DESTINATION "${ProjName_INSTALL_CMAKEDIR}"
        COMPONENT ProjName_Development)

I wrote a blog post with an expanded version of this a while back: https://alexreinking.com/blog/building-a-dual-shared-and-static-library-with-cmake.html

In general, setting an install() destination to anything other than "${SOME_CACHE_VARIABLE}" is bound to cause headaches for some package maintainer. Where GNUInstallDirs doesn't provide a valid configuration point, you must create your own.

瀞厅☆埖开 2025-01-25 14:01:27

我会赞成 ${LIB_INSTALL_DIR}/cmake/myproj

如果您要安装到某个特定于库的安装位置,例如 /opt/myproj - 那么它实际上并不那么重要。但想一想当您安装到 /usr/local 时会发生什么。

  • 如果您将脚本放在 ${LIB_INSTALL_DIR} 中,该库现在将充满 foo-config.cmakefoo-version-config.cmake >,而不仅仅是库文件(和一些子目录)。浏览和搜索的乐趣减少。
  • 如果将脚本放在 ${LIB_INSTALL_DIR}/myproj/cmake 中,那么 - 会发生同样的事情,但使用的是每个项目的子目录而不是文件集。也许更好,但相反 - 为什么我们不直接替换 myprojcmake 的路径元素,这样我们就会得到 cmake/ 目录。恕我直言,这更干净、更方便。

I'll argue in favor of ${LIB_INSTALL_DIR}/cmake/myproj.

If you're installing to some library-specific install location, e.g. /opt/myproj - then it doesn't really matter all that much anyway. But think about what happens when you install to, say, /usr/local.

  • If you place the scripts in ${LIB_INSTALL_DIR}, that library now becomes full of foo-config.cmake and foo-version-config.cmake, instead of just library files (and some subdirs). Less fun for browsing and searching.
  • If you place the scripts in ${LIB_INSTALL_DIR}/myproj/cmake, then - the same thing happens, but with per-project subdirs instead of sets of files. Better, perhaps, but instead - why don't we just replace the path elements of myproj and cmake, and that way we would get a cmake/ directory with many subdirs, instead. That's cleaner and more convenient IMHO.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文