C编程:预处理器,包括宏的文件 [英] 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;
}
编辑:
我很欣赏这些回复,我的例子似乎导致了误导,所以这就是我实际上要解决的问题:
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.
推荐答案
不幸的是,这是超出C预处理器的功能。
Unfortunately, this is beyond the capabilities of the C preprocessor.
您是否考虑使用类似 m4 而不是?
Have you considered using something like m4 instead?
这篇关于C编程:预处理器,包括宏的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!