本文介绍了C 编程:预处理器,包含宏中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我能找到一种方法来做类似的事情,我可以在我的应用程序中删减数百行代码,并显着提高可维护性.有人有什么想法吗?

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 编程:预处理器,包含宏中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 03:51