How to auto-include all headers in directory

前端 未结 10 3202
被撕碎了的回忆
被撕碎了的回忆 2021-02-20 12:32

I\'m going through exercises of a C++ book. For each exercise I want to minimize the boilerplate code I have to write. I\'ve set up my project a certain way but it doesn\'t seem

相关标签:
10条回答
  • 2021-02-20 12:57

    Make a master include file containing the names of all the headers you want.

    It's a really bad idea to include *, even if you could.

    0 讨论(0)
  • 2021-02-20 12:58

    writing a makefile rule to pass the name of the executable as a -DHEADERFILE=something parameter to the compiler shouldn't be difficult at all. Something like:

    %.exe : %.h %.cpp main.cpp
        gcc -o $< -DHEADER_FILE=$<F $>
    

    OTOH, I don't know if #include does macro expansion on the filename.

    0 讨论(0)
  • 2021-02-20 13:02

    If you build your code using make, you should be able to do this.

    Can I include all headers in the directory so at least I don't have to change the #include line?

    Change your include line to something like #include <all_headers.h>. Now, you can let your Makefile auto-generate all_headers.h with a target like:

    all_headers.h:
        for i in `ls *.h`; do echo "#include <$i>" >>all_headers.h; done
    

    Make sure that all_headers.h is getting deleted when you 'make clean'.

    Better yet, can I rewrite my solution so that I don't even have to touch main.cpp, without having one file with all the code for every exercise in it?

    You can do this if you abstract away your class with a typedef. In your example, change your class name from E0614 to myClass (or something). Now, add a line to your Makefile underneath the for loop above that says echo "typedef "$MY_TYPE" myClass;" >>all_headers.h. When you build your program, invoke 'make' with something like make MY_TYPE=E0614 and your typedef will be automatically filled in with the class you are wanting to test.

    0 讨论(0)
  • 2021-02-20 13:03

    try this:-

    #ifndef a_h
    #define a_h
    
    #include <iostream>
    #include <conio.h>
    #incl....as many u like
    class a{
    f1();//leave it blank
    int d;
    }
    #endif //save this as a.h
    

    later include this in ur main program that is cpp file

    #include "a.h"
    

    ...your program

    0 讨论(0)
提交回复
热议问题