使用CMake生成基于目录内容的头/代码的方法
Way to use CMake to generate header/code based on contents of a directory?
假设我有一个类" base ",在子目录" dir "中我有" foo ", " bar "answers" leg ",每个都有一个头文件和一个源文件,继承了" base ",像这样。
-base.hpp/cpp
-dir
|-foo.hpp/cpp
|-bar.hpp/cpp
|-leg.hpp/cpp
我想知道是否有一种方法与Cmake在"dir"中找到头,将它们包含在一个文件中,然后取名称(没有扩展名),然后生成代码,使结果文件类似:
dir_files.hpp——
#include “dir/foo.hpp”
#include “dir/bar.hpp”
#include “dir/leg.hpp”
void function();
dir_files.cpp——
#include “dir_files.hpp”
void function()
{
do_something(foo);
do_something(bar);
do_something(leg);
}
您可以使用以下关键字/技术:
CMake:
# "file" to find all files relative to your root location
file(GLOB SRC_H
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
"dir/*.h"
)
file(GLOB SRC_CPP
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
"dir/*.cpp"
)
# foreach to iterate through all files
foreach(SRC_H_FILE ${SRC_H})
message("header ${SRC_H_FILE}")
# You could build up your include part here
set(INCLUDE_PART "${INCLUDE_PART}#include <${SRC_H_FILE}>n")
endforeach()
foreach(SRC_CPP_FILE ${SRC_CPP})
message("src ${SRC_CPP_FILE}")
endforeach()
message("${INCLUDE_PART}")
# Configure expands variables in a template file
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/HeaderTemplate.h.in.cmake"
"${CMAKE_BINARY_DIR}/HeaderTemplate.h"
)
HeaderTemplate.h.in.cmake:
// Template file
@INCLUDE_PART@
void function();
CMake的输出将是:
日志:header dir/Test1.h
header dir/Test2.h
header dir/Test3.h
src dir/Test1.cpp
src dir/Test2.cpp
src dir/Test3.cpp
#include <dir/Test1.h>
#include <dir/Test2.h>
#include <dir/Test3.h>
HeaderTemplate.h
// Template file
#include <dir/Test1.h>
#include <dir/Test2.h>
#include <dir/Test3.h>
void function();
相关文章:
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 如何重写全局方法名称以在调用原始方法之前将我的代码推到前面
- 在nodejs中使用本机代码的最佳方法是什么?
- 函数从唯一代码调用正确的子类方法
- 如何启用使用另一个类的静态方法的模板函数的自动推导,从而消除冗长的调用方代码
- 有没有更好的方法来编写这个c++代码
- 终端进程终止,退出代码为:1有什么方法可以修复它吗
- 有没有一种预处理器的方法可以从调试符号中删除代码段
- C++:销毁此代码的最佳方法是什么?
- 在.cpp文件中定义方法而不是在 C++ 的 .h 文件中定义方法时,如何提高代码的性能?
- 为什么在我的代码中没有调用 g 模拟方法
- 设计许多单例代码结构的更好方法
- 两种专用方法中的相同代码
- Android 无法从本机代码调用 Java 方法 JNI
- 有什么方法可以从Visual Studio 2017中的Catch C++测试中获取代码覆盖率指标?
- C++11 中的随机数:有没有一种简单的方法可以将生成器种子放在代码的一个位置,然后在不同的函数中使用它?
- 在没有额外代码的情况下链接两个独立类的最通用方法是什么?
- QTimer timeout()方法不再在代码重组后启动