如何在cmake中使用CHECK_INCLUDE_FILES宏? [英] How to use CHECK_INCLUDE_FILES macro in cmake?

查看:1182
本文介绍了如何在cmake中使用CHECK_INCLUDE_FILES宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将我的程序链接到Kerberos身份验证库( gssapi_krb5 )和相应的标头 gssapi / gssapi.h 以及包含在源文件中的 gssapi / gssapi_krb5.h

I need link my program against Kerberos authentication library (gssapi_krb5) with the corresponding headers gssapi/gssapi.h and gssapi/gssapi_krb5.h included in the source file.

当前,如果没有标题,编译将继续进行然后停止,出现编译时错误,提示找不到头文件。
我要在cmake文件中实现的是检查头文件的存在,如果找不到则停止编译。

Currently, the compilation will continue if headers are absent and stop with a compile time error saying header files not found. What I want to implement in the cmake file is to check the existence of the header file and stop compiling if not found.

我将以下代码添加到其中我的 CMakeList.txt 文件。

I add the following code into my CMakeList.txt file.

INCLUDE(CheckIncludeFiles)

CHECK_INCLUDE_FILES(gssapi/gssapi.h;gssapi/gssapi_krb5.h HAVE_KRB_HEADERS)
IF (NOT HAVE_KRB_HEADERS)
    RETURN()
ENDIF (NOT HAVE_KRB_HEADERS)

但是它仍然没有达到我的预期。
我想要以下几行:

But it still does not act as I expected. I would like the following lines:

-- Looking for gssapi/gssapi.h - found
-- Looking for gssapi/gssapi_krb5.h - not found

但失败。
另外,当使用 message 宏输出时,变量 HAVE_KRB_HEADERS 为空。
继续编译,直到发生上述错误为止。

but fail. Also, the variable HAVE_KRB_HEADERS is empty when output with message macro. Compile continues until the error described above occurs.

我在Web上的某个地方阅读过,这可能是因为CMake缓存。
我对CMake还是很陌生,对这个概念还不太清楚。
我的CMake版本是2.6。
如何使此代码起作用?谢谢!

I read somewhere on the Web, this may be because CMake cache. I'm very new to CMake and not quite clear with that concept. My CMake version is 2.6. How could I make this code work? Thank you!

推荐答案

我不能说我是 CheckIncludeFiles 因为它很难正确。原则上讲,这很好-它实际上创建了微小的C文件,这些文件 #include 所请求的标头并尝试对其进行编译,但似乎很容易出错。

I can't say I'm a huge fan of CheckIncludeFiles because of its difficulty to get right. In principal it's good - it actually creates tiny c files which #include the requested headers and tries to compile them, but it seems to be too easy to get wrong.

我通常更喜欢仅使用 find_path 和/或 find_file 。这不会检查找到的任何文件的内容,但是通常,如果找到所需的标头,它的内容就不错了!

I generally prefer just using find_path and/or find_file for this job. This doesn't check the contents of any files found, but usually if you find the required header, its contents are good!

我会使用 find_path 如果我需要知道标题所在的文件夹。这通常是因为我需要检查同一文件夹中的其他文件(与您的情况相同),或更常见的原因是我需要将该文件夹添加到 include_directories 调用。

I would use find_path if I needed to know the folder where the header lived. This would usually be because I need to check for other files in the same folder (as in your case), or more commonly because I need to add the folder to an include_directories call.

find_file 产生文件的完整路径(如果找到)。对于标题,通常我不需要CMakeLists中的其他路径-它只是在 find_file 之后立即使用,以检查是否实际找到了文件。

find_file yields the full path to the file (if found). For headers, normally I don't need the path elsewhere in the CMakeLists - it's just used immediately after the find_file to check the file was actually found.

所以,这就是我要检查 gssapi / gssapi.h和 gssapi / gssapi_krb5.h的方法。

So, here's how I'd go about checking for "gssapi/gssapi.h" and "gssapi/gssapi_krb5.h"

find_path(GssApiIncludes gssapi.h PATHS <list of folders you'd expect to find it in>)
if(NOT GssApiIncludes)
  message(FATAL_ERROR "Can't find folder containing gssapi.h")
endif()

find_file(GssKrb gssapi_krb5.h PATHS ${GssApiIncludes} NO_DEFAULT_PATH)
if(NOT GssKrb)
  message(FATAL_ERROR "Can't find gssapi_krb5.h in ${GssApiIncludes}")
endif()

如果执行此操作,则可以根据需要添加

If you do this, then if required you could add

include_directories(${GssApiIncludes})

因此您可以在源代码中执行

so that in your source code you can do

#include "gssapi.h"
#include "gssapi_krb5.h"

这篇关于如何在cmake中使用CHECK_INCLUDE_FILES宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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