Valhalla Legends Forums Archive | C/C++ Programming | Organizational Problem?

AuthorMessageTime
NicoQwertyu
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.
September 27, 2005, 7:05 PM
Yegg
Why not use one header file for all globally used variables and/or functions? Correct me if this is not what you meant.
September 27, 2005, 7:19 PM
Myndfyr
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:

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

Then you can pass that structure or pointers to it around.
September 27, 2005, 10:55 PM
Kp
[quote author=Yegg link=topic=12921.msg129478#msg129478 date=1127848745]
Why not use one header file for all globally used variables and/or functions? Correct me if this is not what you meant.
[/quote]

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.
September 28, 2005, 1:40 AM
NicoQwertyu
Thanks guys.  I didn't know about extern definitions when I asked this.  I'm back on track now.  :P
September 28, 2005, 11:30 AM

Search