如何在CMAKE_MODULE_PATH中使用相对路径?

huangapple go评论87阅读模式
英文:

How can I use relative paths in CMAKE_MODULE_PATH?

问题

我的项目路径是:

C:/项目/base/产品A

在其CMakeLists.txt文件中,我设置了CMAKE_MODULE_PATH的路径为:
C:/项目/base/工具/etc/cmake/模块

我设置了绝对路径,它能正常工作。

set(CMAKE_MODULE_PATH "C:/项目/base/工具/etc/cmake/模块;${CMAKE_MODULE_PATH}")

如何设置相对路径:类似于“..工具/etc/cmake/模块”
我尝试了相对路径,但不起作用(抛出错误,只有绝对路径起作用)。

英文:

My Project path is:

C:/Projects/base/ProductA

In its CMakeLists.txt I set CMAKE_MODULE_PATH which path is:
C:/Projects/base/Util/etc/cmake/Modules

I set an absolute path and it works.

set(CMAKE_MODULE_PATH "C:/Projects/base/Util/etc/cmake/Modules;${CMAKE_MODULE_PATH}")

How to set relative path: something like "..Util/etc/cmake/Modules"
I tried relative, but it doesn't work (error thrown, so only absolute paths work).

答案1

得分: 1

似乎CMAKE_MODULE_PATH不支持相对路径。关于此问题有一个相关的邮件线程 这里。一些命令支持相对路径,比如 add_subdirectoryadd_executableadd_librarytarget_sources。通常情况下,如果支持相对路径,文档会明确说明,并说明在这些情况下相对于哪里解析。还可参考一些相关的Wiki常见问题,比如 为什么CMake使用完整路径,我能复制我的构建树吗?

有很多变量可以用来锚定路径到其他绝对目录,比如 CMAKE_SOURCE_DIRPROJECT_SOURCE_DIRCMAKE_CURRENT_SOURCE_DIRCMAKE_CURRENT_LIST_DIR

例如,如果 PROJECT_SOURCE_DIRC:/Projects/base/ProductA,那么你可以使用 list(PREPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/../Util/etc/cmake/Modules;${CMAKE_MODULE_PATH}")。注意:这感觉有点奇怪。如果 Util 项目在一个单独的仓库中,你基本上要求项目的用户在特定的位置相对彼此克隆东西,这比我在自己的项目中更紧密耦合。我会给用户一个可以设置的变量,指向 Utils 项目克隆的位置。

英文:

It seems that relative paths are not supported by CMAKE_MODULE_PATH. There's a related mail thread on this here. Some commands do, such as add_subdirectory, or add_executable / add_library / target_sources. Usually, when relative paths are supported, the docs will explicitly say so, and say what they are resolved relative to in those cases. See also some related wiki FAQs, such as Why does CMake use full paths, or can I copy my build tree?

There are plenty of variables you can use to anchor a path to some other absolute directory path, such as CMAKE_SOURCE_DIR, PROJECT_SOURCE_DIR, CMAKE_CURRENT_SOURCE_DIR, and CMAKE_CURRENT_LIST_DIR.

Ex. if PROJECT_SOURCE_DIR is C:/Projects/base/ProductA, then you could use list(PREPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/../Util/etc/cmake/Modules;${CMAKE_MODULE_PATH}"). Note: That feels a little weird though. If the Util project is in a separate repo, you're basically requiring users of the project to clone things in specific locations relative to each other, which is more tightly coupled than I would do in my own projects. I'd instead give users a variable they can set which points to where the Utils project is cloned to.

huangapple
  • 本文由 发表于 2023年5月24日 23:04:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定