Project/File design

Thread Solved

Join Date: Apr 2006
Posts: 353
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Project/File design

 
0
  #1
Dec 29th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 353
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Project/File design

 
0
  #2
Dec 30th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,563
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Project/File design

 
0
  #3
Dec 31st, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 353
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Project/File design

 
0
  #4
Dec 31st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 353
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Project/File design

 
0
  #5
Jan 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Project/File design

 
0
  #6
Jan 1st, 2009
Originally Posted by death_oclock View Post
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 */
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 353
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Project/File design

 
0
  #7
Jan 1st, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 353
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Project/File design

 
0
  #8
Jan 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Project/File design

 
0
  #9
Jan 1st, 2009
Originally Posted by death_oclock View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 353
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Project/File design

 
0
  #10
Jan 1st, 2009
Sorry, my terminology is a little off. I meant just the function header, with no body or code.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC