English 中文(简体)
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.

最佳回答

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"

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.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签