fedora
fedora

Reputation: 201

Conventional c++ program header file structure?

I know the mechanics of how header files work in c++ but i'm just starting to write bigger opengl programs with lots of classes and i was wondering if there is a "standard" or conventional way of organizing larger programs around header files or if it is just personal preference for the most part?

Upvotes: 4

Views: 459

Answers (2)

Steve Rowe
Steve Rowe

Reputation: 19413

For very large projects there are usually 2 kinds of functions/objects: those that are used in a lot of places and those that are used only locally (in one file or a handful of related files). You want to put these into two different locations:

  • In a central place, say root\inc, put the headers for all of the functions and constants that are used widely here.
  • In each directory, put the header files that only affect those areas.

The advantage of this is that it forces some decoupling of your program. A cpp file in the UI portion can't access the functions in the networking portion unless it is in the central place and expected to be widely used.

If you are talking not quite so large but still more than one file, then the rule of thumb is you keep each header file to one class or at most couple highly related classes.

Upvotes: 1

Arunmu
Arunmu

Reputation: 6901

1. Divide your project into Modules.
2. Make a directory for each module.
3. Under each directory for a module create 2 sub directories inc/ and src/
where you will place your header and source files resp.
4. Create a global directory and place your header and cpp files in that directory which 
would be used commonly by several modules.
5. Make your Makefile that includes path and links to all libraries correctly.

This is what I follow. There may be other more efficient and less efficient ways :)

Upvotes: 5

Related Questions