Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: NicoQwertyu on September 27, 2005, 02:05 PM

Title: Organizational Problem?
Post by: NicoQwertyu on September 27, 2005, 02:05 PM
I don't know how to explain the issue I'm having, but as my projects get slightly bigger, I start splitting certain functions into different files.

For instance:
main.c
chat.c
chat.h
connection.c
connection.h
commands.c
commands.h
fileio.c
fileio.h


The problem occurs when I start needing files to share certain variables (like socket descriptors) and I just start to get very sloppy.  If anyone a little more experienced with c has any tips for organizing my projects better, please help me out.
Title: Re: Organizational Problem?
Post by: Yegg on September 27, 2005, 02:19 PM
Why not use one header file for all globally used variables and/or functions? Correct me if this is not what you meant.
Title: Re: Organizational Problem?
Post by: MyndFyre on September 27, 2005, 05:55 PM
You could use structures for things that are common and pass those around.  For example, you might have:
** a HFILE for a log
** a SOCKET for a socket
** a HWND for a rich text box window

All of these objects could be grouped together in a single structure that is used on a per-connection basis:


typedef struct tagConnectionParams {
  int id;
  SOCKET socket;
  HWND hRichTextBoxWnd;
  HFILE hLogFile;
} CONNECTION_PARAMS, *PCONNECTION_PARAMS;


Then you can pass that structure or pointers to it around.
Title: Re: Organizational Problem?
Post by: Kp on September 27, 2005, 08:40 PM
Quote from: Yegg on September 27, 2005, 02:19 PM
Why not use one header file for all globally used variables and/or functions? Correct me if this is not what you meant.

Although this would work, I strongly recommend NOT doing this.  When you have a project with dozens or hundreds of source files, it's really annoying to have the whole project rebuild because one declaration in one header changed.
Title: Re: Organizational Problem?
Post by: NicoQwertyu on September 28, 2005, 06:30 AM
Thanks guys.  I didn't know about extern definitions when I asked this.  I'm back on track now.  :P