C Programming: Preprocessor, include files from macro

青春壹個敷衍的年華 提交于 2019-12-02 04:30:54

Unfortunately, this is beyond the capabilities of the C preprocessor.

Have you considered using something like m4 instead?

Why not just create a header file that itself #includes all of the other header files for the library? 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. 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.

In file includes.h

#include "1.h"
#include "2.h"
#include "3.h"

In all other files

#include "includes.h"
mjv

The #include pre-process directive being itself handled at in the same step as the macro evaluation, this would likely not work. 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. See Cory Petosky's reponse for an illustration.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!