1

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 don't care at which order the functions are included.

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

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Newbie
  • 1,593
  • 10
  • 35
  • 50
  • 5
    Quick & (probably) dirty : You make a "*.h" with a list of include with all your files. Then you include this "*.h". – Raveline Sep 20 '10 at 11:10
  • 1
    _Why_ did you not properly group your functions, so that including now is a PITA? – sbi Sep 20 '10 at 11:14
  • 2
    @Raveline, your comment could be the potential answer.. :) – liaK Sep 20 '10 at 11:17
  • possible duplicate of [C++: include all files in a directory?](http://stackoverflow.com/questions/3061582/c-include-all-files-in-a-directory) – Björn Pollex Sep 20 '10 at 11:22
  • @sbi, as told below, all my functions belongs to the same group. – Newbie Sep 20 '10 at 11:40
  • #include"somedir/*.h" didnt work. – Newbie Sep 20 '10 at 11:40
  • If you have all your include files in one folder do Tools->Options->Projects&Solutions->VC++Directories and add the include folder in include directories – DumbCoder Sep 20 '10 at 11:52
  • so every project i do on my visual-studio-2008 would include those files? – Newbie Sep 20 '10 at 12:07
  • 1
    @Newbie: If they are all in the same group, you have some extremely poor header design. – Puppy Sep 20 '10 at 12:30
  • @Newbie: Then they should be in the same header. – sbi Sep 20 '10 at 14:33
  • i dont understand what you mean by poor header design. you suggest me to put all these 500 functions in one file and include this file once in my project? instead of saying im doing things wrong you could just tell how to do things right instead and i may accept that as correct answer – Newbie Sep 20 '10 at 16:48
  • im not including header files.. i dont even know exactly whats the proper way of including files. but i do know this: i write 4 functions in one file, i want to include those functions in my project so i can use them in my program, so how do i do this? i write manually 100 lines of #include's in my project, and every time i rename some file or add more files, i need to edit the #includes manually again. – Newbie Sep 20 '10 at 16:53
  • i dont even use .h files to declare my functions since its a pain in the ass to maintain if i add/edit new parameters in my 1000 functions – Newbie Sep 20 '10 at 16:56

4 Answers4

8

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
  • 26,407
  • 13
  • 68
  • 88
  • 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 Sep 20 '10 at 11:39
  • No, this solution allows you to include just all.h, which will in turn pull in all of the headers listed. – James Broadhead Sep 20 '10 at 11:48
  • @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. Sep 20 '10 at 11:53
  • 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 Sep 20 '10 at 11:59
3

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.
  • 2,890
  • 1
  • 18
  • 21
2
  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
  • 15,488
  • 4
  • 32
  • 41
0

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
  • 13,974
  • 11
  • 61
  • 86
  • 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 Sep 20 '10 at 16:44
  • so you suggest me to put these 500 functions in one file again? that will be horrible to edit – Newbie Sep 20 '10 at 16:51
  • @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 Sep 20 '10 at 17:04
  • @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 Sep 20 '10 at 17:09
  • so if i make cpp files, they are included automatically? – Newbie Sep 20 '10 at 19:06
  • @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 Sep 21 '10 at 08:11
  • 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 Sep 21 '10 at 14:55