How to auto-include all headers in directory

前端 未结 10 3279
被撕碎了的回忆
被撕碎了的回忆 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 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 . 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.

提交回复
热议问题