943,852 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2196
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 29th, 2008
0

Project/File design

Expand Post »
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?
Similar Threads
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Dec 30th, 2008
0

Re: Project/File design

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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Dec 31st, 2008
0

Re: Project/File design

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.
Last edited by BestJewSinceJC; Dec 31st, 2008 at 12:28 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Dec 31st, 2008
0

Re: Project/File design

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
  1. void doSomething()
in the top of header1.h
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Jan 1st, 2009
0

Re: Project/File design

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.
Last edited by death_oclock; Jan 1st, 2009 at 3:46 am.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Jan 1st, 2009
0

Re: Project/File design

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:
  1. /* .c file brief description */
  2. #include "common.h"
  3.  
  4. #include "theme1.h"
  5. #include "utility.h"
  6.  
  7. /* theme1 function definitions */
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jan 1st, 2009
0

Re: Project/File design

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?
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Jan 1st, 2009
0

Re: Project/File design

Oh man, I just realized a pretty major mistake I made in a previous explanation.
Quote ...
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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Jan 1st, 2009
0

Re: Project/File design

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?
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jan 1st, 2009
0

Re: Project/File design

Sorry, my terminology is a little off. I meant just the function header, with no body or code.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Windows Registry problem
Next Thread in C Forum Timeline: Query related to Hiding symbol in Shared Library





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC