在CMakeCache.txt中指定INCLUDE_PATH时,如何告诉CMake递归搜索包含目录

时间:2018-10-26 17:16:51

标签: cmake ncurses

我最近使用源代码构建了ncurses-6.1,并希望配置自己的STIR(断层图像重建软件)构建以使用最新版本的ncurses。但是,我不知道如何告诉cmake在include文件夹中搜索子目录。指定CMakeCache.txt文件中的ncurses包含路径时:

  

CURSES_INCLUDE_PATH:PATH = / home / matthew / Programs / ncurses / ncurses-6.1-build / include

在我的STIR-build目录中运行make时,我收到以下错误:

In file included from /home/matthew/Programs/stir/STIR/src/display/gen.c:21:0:
/home/matthew/Programs/stir/STIR/src/display/gen.h:111:10: fatal error: curses.h: No such file or directory
 #include <curses.h>             /* for getch */
          ^~~~~~~~~~
compilation terminated.

但是,如果我指定子目录:

  

CURSES_INCLUDE_PATH:PATH = / home / matthew / Programs / ncurses / ncurses-6.1-build / include / ncurses

然后我收到以下错误:

In file included from /home/matthew/Programs/stir/STIR/src/display/gen.h:111:0,
                 from /home/matthew/Programs/stir/STIR/src/display/screengen.c:23:
/home/matthew/Programs/ncurses/ncurses-6.1-build/include/ncurses/curses.h:60:10: fatal error: ncurses/ncurses_dll.h: No such file or directory
 #include <ncurses/ncurses_dll.h>
          ^~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

当我将头文件从子目录直接复制到包含文件夹(与子目录一起)时,构建成功。如何告诉cmake递归查看子目录?

1 个答案:

答案 0 :(得分:0)

cmake不能直接提供这种功能。您可以编写一个file(GLOB_RECURSE ..)命令以将给定目录下的所有目录收集到一个列表中,然后基于该列表发出include_directories()命令。例如

set(myDesiredRootForHeaderFileSearch ~/special/include/path)
file(GLOB my_include_directories ${myDesiredRootForHeaderFileSearch})
include_directories(${my_include_directories})

GlOB命令获取目录,所以很好。

您还可以使用在论坛上找到的这个非常有用的宏:

MACRO(HEADER_DIRECTORIES return_list)
    FILE(GLOB_RECURSE new_list *.h)
    SET(dir_list "")
    FOREACH(file_path ${new_list})
        GET_FILENAME_COMPONENT(dir_path ${file_path} PATH)
        SET(dir_list ${dir_list} ${dir_path})
    ENDFOREACH()
    LIST(REMOVE_DUPLICATES dir_list)
    SET(${return_list} ${dir_list})
ENDMACRO()

HEADER_DIRECTORIES(header_list)

include_directories(${header_list}

如本文Link to post

所建议

我还没有测试过。让我知道是否行得通。