gpt4 book ai didi

c++ - 使用 CMake 创建包含文件

转载 作者:搜寻专家 更新时间:2023-10-31 00:32:48 24 4
gpt4 key购买 nike

我有一个包含 hpp 和 cpp 文件的文件夹。我需要创建一个头文件,其中包含上述文件夹中的所有 hpp 文件:

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

CMake 能做到吗?

注意:我不会把“我的”研究工作放在你的肩上。我只需要知道这样的事情是否可能,以及可能指向我可以阅读的地方的链接。我最初的谷歌搜索没有显示任何有用的信息。谢谢

最佳答案

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

list.hpp.in

#pragma once
@include_statements@

然后,在您的 CMakeLists.txt 中,您可以用 #include 语句包裹的文件列表填充占位符 @include_statements@ .

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)

关于c++ - 使用 CMake 创建包含文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30499526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com