I am having some trouble with the layout of my project. I'm using several APIs, including the WinAPI and DevIL. I had hoped to encapsulate each one in a separate header file, so that the main program would never have to know whats going on. The problem is, some functions have to refer to each other in different files. Is there any way I can let them do this and still keep the encapsulation?

I started experimenting with all sorts of bizarre extern statements and separate .c files but I realized it would be a lot easier to require the client (main) to call the subsequent functions, rather than have them call each other. It will require more parameters but it seems to be much better style anyway.

It's not necessarily better style/design. And if the job of one function requires another function, then you should call the other function within the first function. You shouldn't use main to do it. And I'm kind of curious how to do this as well, which is why I am upping this topic. Its been a while since I've programmed in C, but I would think you'd just include the one header in the other one. Since you need to compile each file into a .o, I'm not sure where that comes into play though. Guess you'd just always have to link them somehow.

I had a thought, what if you just declared the function you need to reference in the top of the file that uses it? If file header1.h needs function doSomething() which is defined in another header, put

void doSomething()

in the top of header1.h

Sorry for all the stupid reposts, but you can only edit for so long. Anyway, I refined my previous idea. Just put all the function definitions in one header file and have all the other headers include it. That way every function knows about every other function.

Just put all the function definitions in one header file and have all the other headers include it. That way every function knows about every other function.

That's the worst solution. Now you have a huge scrap heap of all possible interfaces. It's hard to maintain such a heap, you have drastically increased compilation time and full project recompilation overheads on every new prototype added.

Look at extremelly rational C library headers structure. Group all your global functions, typedefs and global macros by themes. Place related declarations in correspondent .h files. Include only needed .h files in .c modules. Never mix unrelated function definitions in a single .c file.

If the most of your functions use common (as usually system) headers, place correspondent includes in the single common .h file (common.h, for example). Try to use precompiled headers feature of your compiler. For example, in VC++ place all system header include directives in stdafx.h.

Now your include police looks like:

/* .c file brief description */
#include "common.h"

#include "theme1.h"
#include "utility.h"

/* theme1 function definitions */

I didn't quite understand that. I get the idea of using a common header for system includes. But the rest I wasn't sure of. Am I right in thinking this: include all formal function declarations first, then all their definitions?

Oh man, I just realized a pretty major mistake I made in a previous explanation.

Just put all the function definitions in one header file and have all the other headers include it.

I meant delcarations. Only the function headers would be in this file.

I didn't quite understand that. I get the idea of using a common header for system includes. But the rest I wasn't sure of. Am I right in thinking this: include all formal function declarations first, then all their definitions?

I don't understand what's "formal function declaration". Have you ever seen informal function declarations?

Sorry, my terminology is a little off. I meant just the function header, with no body or code.

I'm really not sure anymore. Now I just declare all my functions before I define any of them. It works and I can't see it being a poor solution.

This question has already been answered. Start a new discussion instead.