2015-05-28 68 views
2

我有一个包含hpp和cpp文件的文件夹。我需要创建一个包含所述文件夹中所有hpp文件的头文件:使用CMake创建包含文件

#pragma once 
#include "test_1.hpp" 
#include "test_2.hpp" 
#include "test_3.hpp" 

可以CMake做到这一点吗?

注:我不打算把'我的'研究工作放在你的肩膀上。我只需要知道这样的事情是否可能,以及可能的链接到我可以阅读的地方。我最初的Google搜索没有显示任何有用的信息。 谢谢

回答

5

您可以使用configure_file来填充模板文件中的变量。创建一个包含所需文件的轮廓和您的包含语句的占位符,例如模板文件:

list.hpp.in

#pragma once 
@[email protected] 

然后,在你的CMakeLists.txt,你可以使用包含#include语句的文件列表填充占位符@[email protected]

project(test) 
cmake_minimum_required(VERSION 2.8) 

# Get list of some *.hpp files in folder include 
file(GLOB include_files include/*.hpp) 

# Convert the list of files into #includes 
foreach(include_file ${include_files}) 
    set(include_statements "${include_statements}#include \"${include_file}\"\n") 
endforeach() 

# Fill the template  
configure_file(list.hpp.in list.hpp) 
+1

通常的做法是将.in扩展名添加到cmake使用的模板中。在你的情况configure_file(list.hpp.in list.hpp)它可以更容易地将输入与输出相关联。 – ypx

+0

好点,编辑。 –

+0

请记住,此解决方案需要运行CMake以反映添加到include目录的任何新文件。 – ypx