[英]C Programming: Preprocessor, include files from macro
If I could find a way to do something similar to this, I could cut out hundreds of lines of code in my application, and dramatically increase maintainability. 如果我可以找到一种类似的方法,那么我可以在应用程序中减少数百行代码,并大大提高可维护性。 Anyone have any ideas?
有人有想法么?
#include <stdio.h>
int main( )
{
#define include_all_files(root) \
#include #root "1.h" \
#include #root "2.h" \
#include #root "3.h" \
#include #root "4.h"
include_all_files( first_library )
include_all_files( second_library )
include_all_files( third_library )
return 0;
}
EDIT : 编辑 :
I appreciate the responses, my example seems to be causing a misdirection in effort, so here is the problem I am actually trying to solve: 我感谢您的回答,我的示例似乎造成了错误的方向,所以这是我实际上要解决的问题:
I am implementing a finite state machine. 我正在实现一个有限状态机。 Through naming conventions, I have gotten it to be as simple to add a state as:
通过命名约定,添加状态变得如此简单:
STATE(initial_screen)
#include "initial_screen.state"
END_STATE
STATE(login)
#include "login.state"
END_STATE
However, if I could answer the original question, I could refactor this down to something as simple as: 但是,如果我可以回答原始问题,则可以将其重构为以下简单内容:
ADD_STATE(initial_screen)
ADD_STATE(login)
This is because the file name and the state name, and all the underlying wiring and everything else all follow similar conventions. 这是因为文件名和状态名,以及所有底层连接以及其他所有内容都遵循相似的约定。 However, I cannot figure out how to implement the include file based on the token received in a macro.
但是,我无法弄清楚如何基于宏中接收的令牌来实现包含文件。
Why not just create a header file that itself #include
s all of the other header files for the library? 为什么不创建一个头文件本身
#include
一切都会库中的其他头文件的? Then for each library you'd just include that library's one meta-header. 然后,对于每个库,您只需添加该库的一个元标题即可。
To solve your clarified problem, you could just refactor initial_screen.state
and login.state
so that they start with STATE()
and end with END_STATE
. 为了解决您所澄清的问题,您只需重构
initial_screen.state
和login.state
使它们以STATE()
开头并以END_STATE
结束。 Then you can just do: 然后,您可以执行以下操作:
#include "initial_screen.state"
#include "login.state"
...which is equivalent to what you're after - it's just an #include
instead of an ADD_STATE
. ...相当于您所需要的-它只是
#include
而不是ADD_STATE
。
In file includes.h 在文件include.h中
#include "1.h"
#include "2.h"
#include "3.h"
In all other files 在所有其他文件中
#include "includes.h"
The #include pre-process directive being itself handled at in the same step as the macro evaluation, this would likely not work. #include预处理指令本身在与宏评估相同的步骤中处理,因此可能无法正常工作。 I don't believe the pre-processing can be recursive.
我不认为预处理可以是递归的。 (or iterative, for that matter).
(或对此进行迭代)。
Instead, the typical way this is done is to create a small include file which includes all the desired #includes. 取而代之的是,完成此操作的典型方法是创建一个包含所有所需的#includes的小型包含文件。 See Cory Petosky's reponse for an illustration.
请参阅Cory Petosky的回复以获取插图。
A word of caution : 请注意 :
While this may cut hundreds of line of code, you should consider that these are "easy" lines of code . 尽管这可能会减少数百行代码,但是您应该考虑这些是“简单”的代码行 。 One typically skims over them, unlike lines of codes associated with the true logic of the program.
与程序的真实逻辑相关联的代码行不同,通常会跳过它们。
Furthermore, explicitly listing the individual includes necessary for a given file provide a bit of self documentation, and make it easier for refactoring the code when needed. 此外,显式列出给定文件所需的个人包括的内容可提供一些自我文档,并在需要时使重构代码更加容易。
Edit : This just in ;-) 编辑 :这只是在;-)
Someone just asked this SO question Good idea to put all project headers into one file? 有人刚刚问了这个问题,将所有项目标头放入一个文件中是个好主意吗? , and the responses seem to generally agree with my take that there's typically little gain to be had from grouping headers.
,而且回应似乎大致上与我的看法一致,即分组标头通常没有什么好处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.