Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto-include all headers in directory

Tags:

c++

include

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 right, and requires too many changes.

Right now I have a single main.cpp file with the following:

#include "e0614.h"
int main()
{
    E0614 ex;
    ex.solve();
}

Each time I create a new class from an exercise, I have to come and modify this file to change the name of the included header as well as the class i'm instantiating.

So my questions are:

  1. Can I include all headers in the directory so at least I don't have to change the #include line?
  2. 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?

Update:

I ended up following Poita_'s advice to generate main.cpp via a script.

Since I'm using an IDE (Visual Studio), I wanted this integrated with it, so did a bit of research on how. For those interested in how, read on (it was fairly, but not entirely, straightforward).

Visual Studio lets you use an external tool via the Tools -> External Tools menu, and contains a bunch of pre-defined variables, such as $(ItemFileName), which can be passed on to the tool. So in this instance I used a simple batch file, and it gets passed the name of the currently selected file in Visual Studio.

To add that tool to the toolbar, right click on the toolbar, select Customize -> Commands -> Tools, and select the "External Command X" and drag it to the toolbar. Substitute X with the number corresponding to the tool you created. My installation contained 5 default pre-existing tools listed in Tools -> External Tools, so the one I created was tool number 6. You have to figure out this number as it is not shown. You can then assign an icon to the shortcut (it's the BuildMain command shown below):

alt text

like image 522
JRL Avatar asked Mar 12 '10 18:03

JRL


People also ask

HOW include all header files in C++?

You make the declarations in a header file, then use the #include directive in every . cpp file or other header file that requires that declaration. The #include directive inserts a copy of the header file directly into the . cpp file prior to compilation.

Is it correct to include all header files in AC program?

In C program should necessarily contain the header file which stands for standard input and output used to take input with the help of scanf() and printf() function respectively.

Which header file automatically Inclueds in source code?

In computer programming, a precompiled header (PCH) is a (C or C++) header file that is compiled into an intermediate form that is faster to process for the compiler.

What is multiple inclusion of header files?

If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time.


2 Answers

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.

like image 60
bta Avatar answered Oct 21 '22 22:10

bta


  1. No. You have to include them all if that's what you want to do.

  2. No. At least, not in a way that's actually going to save typing.

Of course, you could write a script to create main.cpp for you...

like image 34
Peter Alexander Avatar answered Oct 21 '22 20:10

Peter Alexander