views:

140

answers:

4

I have put each function in own file.

How do i include all those functions at once without repeating #include for each file manually? I dont care at which order the functions are included.

Edit: All the functions from hundreds of different files belongs to the same group. Actually each file has 4 functions...

+1  A: 
  1. You add all the files containing the function definitions (function bodies) to your project
  2. You write one header file that contains a declaration for your functions.
  3. You include that header where needed.
Bart van Ingen Schenau
+7  A: 

Consider your files.

file1.h

int plus(int a, int b);

file2.h

int minus(int a, int b);

file3.h

int mult(int a, int b);

file4.h

void drawcircle(int r, int xc, int yc);

file5.h

void drawsquare(int x0, int y0, int x1, int y1);

file6.h

void printresults();

Now divide your files into groups. Make the following files.

math_funcs.h

#include "file1.h"
#include "file2.h"
#include "file3.h"

draw_funcs.h

#include "file4.h"
#include "file5.h"

output_funcs.h

#include "file6.h"

Then make all.h file.

all.h

#include "math_funcs.h"
#include "draw_funcs.h"
#include "output_funcs.h"
Alexey Malistov
uh... all the functions belong to same group, and this wouldnt solve the manual including problem. I am going to have to include about hundred of files in the future, and i must write each of the #include lines manually, which sucks.
Newbie
No, this solution allows you to include just all.h, which will in turn pull in all of the headers listed.
James Broadhead
@Newbie: each time you write a file, you write ONE include in "something.h" (and perhaps ONE include for "something.h" in "all.h"). It really is the best solution.
Matthieu M.
so who will update my all.h file?
Newbie
I dont want to update something that is not important in any way... i dont care which order the include files are, and there is going to be hundreds of them
Newbie
+1  A: 

Use a prebuild step and execute a batch file like the following:

@echo off
setlocal 
set file=test.h
echo // generated > %file%
for %%f in (*.h) do echo #include "%%f" >> %file%

Then include test.h.

In VS2008 you can add a prebuild step in "Project" - "Properties" - "Configuration Properties" - "Build Events" - "PreBuild Event".

Set "Command Line" to $(ProjectDir)\test.cmd and copy test.cmd (with the above contens) to the project directory.

ur
where do i paste that code?
Newbie
@Newbie: We don't which tools you use to build your software. Use whatever seems comfortable to auto-generate a list of all the .h files.
Tadeusz A. Kadłubowski
im using visual-studio-2008
Newbie
A: 

is it C or C++ question??

In C++, you usually have file per class, not function. And if, after having an .h and .cpp file per class, you still have hundreds of them in one directory, you need to reconsider your design. You probably need more abstraction layers, and you can divide your files in several directories.

davka
At least two reasons for one function, one file rule: Linker Resolution and Propagation of Change. Some linkers have resolution to include or exclude functions only at file level. At some shops, when making changes, there is less of a chance for a change to affect other functions when editing.
Thomas Matthews
so you suggest me to put these 500 functions in one file again? that will be horrible to edit
Newbie
@Newbie: are you saying that you have 500 (!) functions so closely related that you can't classify them into groups and have classes providing related functionality? Out of curiosity, can you give an example? If indeed so, then, as suggested above, have one (or a few) .h files and many .cpp files. Yet, I suggest trying to think in terms of classes rather then functions.
davka
@Thomas: I guess this applies to .cpp files, you will still have the class definition in one .h file and that is more relevant to the question.
davka
so if i make cpp files, they are included automatically?
Newbie
@Newbie: if you use some kind of IDE like MS Visual Studio or Eclipse, the .cpp files are added automatically to the project (or at least you can add them all in one operation). Then, in the place where you want to make use of one of the functions, you include the .h file that contains all function declarations, and the linker works out the rest. In short, you have a file per function definition (body), and one .h file with all function declarations (or several .h files included in one "meta" .h file
davka
oh.. that explains few things lol. just that im not a big fan of updating the .h file function definitions, ive got a lot of annoyance with them... so i dont even define functions separately anymore
Newbie