对于 cmake “包括"命令,文件和模块有什么区别? [英] For the cmake "include" command, what is the difference between a file and a module?

查看:31
本文介绍了对于 cmake “包括"命令,文件和模块有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一些库,我不想将它们构建为每个使用它们的项目的一部分.一个非常容易理解的例子是 LLVM,它有 78 个静态库.即使让 cmake 代码在每个 cmakefile 中查找和导入这些文件也是多余的.

I use some libraries that I don't want built as part of every project that uses them. A very understandable example is LLVM, which has 78 static libraries. Even having the cmake code to find and import these in every cmakefile is excessive.

显而易见的解决方案似乎是使用include"命令,并将相关的 cmake 脚本块分解到 *.cmake 文件中,并设置一个 CMAKE_MODULE_PATH 环境变量.

The obvious solution seems to be to use the "include" command, and to factor out relevant chunks of cmake script into *.cmake files, and set up a CMAKE_MODULE_PATH environment variable.

除非它只是简单的不起作用.Cmake 没有找到我在 include 命令中指定的文件.

Except it just plain doesn't work. Cmake doesn't find the file I specify in the include command.

在偶然的情况下,我什至尝试通过多种方式在环境变量中指定路径 - 一次使用反斜杠,一次使用正斜杠...... - 我每次都重新启动命令提示符并检查环境变量是否存在并且正确.

On the off-chance, I even tried specifying the path in the environment variable several ways - once with backslashes, once with forward slashes... - and I restarted the command prompt each time and checked that the environment variable was present and correct.

在 cmake 手册中,它有点暗示文件"与模块"不同 - 只有模块获得自动添加扩展和搜索路径处理.但是没有解释区别是什么.我猜想缺少的扩展可能就足够了(就像标准模块一样),但显然不是.

In the cmake manual, it kind of implies that a "file" is different from a "module" - only a module gets the automatic add-the-extension-and-search-the-path treatment. But there is no explanation of what the difference is. I guessed that the missing extension might be enough (as with standard modules) but clearly it isn't.

在手册中搜索模块"并没有多大帮助,因为这个词似乎超载了.例如,一个模块也是一个使用 LoadLibrary/dl_open 加载的动态库.

Searching the manual for "module" isn't much help as the word seems to be overloaded. For example, a module is also a dynamic library loaded using LoadLibrary/dl_open.

谁能解释一下在这种情况下文件和模块之间的区别,以及我如何创建自己的模块以便 cmake include 命令可以找到并使用它?

Can anyone explain what the difference is between a file and a module in this context, and how I create my own module so that the cmake include command can find and use it?

我在 Windows 上使用 cmake 2.8.1.

I'm using cmake 2.8.1 on Windows.

编辑

我非常有信心这里的问题是不理解 cmake 应该如何工作.我认为我应该做的是写一些find_package可以使用的东西.

I'm pretty confident that the problem here is not understanding how cmake is supposed to work. I think what I should be doing is writing something that find_package can work with.

尽管如此,我离能够回答我自己的问题还有一段距离.

As things stand though, I'm still some way from being able to answer my own question.

推荐答案

我相信 CMake 的模块"只是一个可以与 find_package 指令.也就是说,当您运行 find_package(Module) 时,它会在 MODULE_PATH 中搜索名为 FindModule.cmake 的文件.

I believe that a CMake 'module' is simply a file that can be used with the find_package directive. That is, when you run find_package(Module), it searches for a file in the MODULE_PATH that is named FindModule.cmake.

也就是说,如果你include一个没有扩展名的文件,它也会在你的MODULE_PATH中搜索那个file.cmake.在我正在处理的 CMake 项目中,我的目录结构与您提出的非常相似.

That said, if you include a file without the extension, it too will search through your MODULE_PATH for that file.cmake. In a CMake project I'm working on, I have a very similar directory structure as to what you propose.

+ root/
  + CMakeLists.txt
  + cmake/
  | + FindMatlab.cmake
  | + TestInline.cmake
  | + stdint.cmake
  + src/
  + include/

在 CMakeLists.txt 我有:

In CMakeLists.txt I have:

set (CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake")
find_package (Matlab) # runs FindMatlab.cmake
include(TestInline) # defines a macro:
test_inline (CONFIG_C_INLINE)
include(stdint) # simply executes flat CMake code

也许您的问题是您试图从环境中定义模块路径.相反,尝试简单地将您尝试访问模块/文件的 CMakeList 附加到.

Perhaps your issue is that you are trying to define the Module path from the environment. Instead, try to simply append to it within the very CMakeList you try to access the modules/files.

这篇关于对于 cmake “包括"命令,文件和模块有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆