question about header files

K

Kevin Joplin

Hi there,
I've almost finished writing simple server application and i have one
little doubt. Suppose we have main server code in server.c file. Rest
of the code we divided into some pices and put into server_2.c,
server_3.c etc. Now we need to share some of the server.c variables
between server_2.c, server_3.c etc. so we put the declaractions of
these variables in server.h with "extern" prefix and include it
(server.h) in rest of the sources. We also put in server.h some consts
describing server, such a port number, version, max lenght of nick
etc. these information are necessary when someone would write a client
application. so in server.h we gathered two types of information :
external declarations for server sources (modules) and const for
future client developers. i used tehnique shown below to separate
these two kinds of adaptation:

in server.h

<cut>
//informations about server
const int SERVER_PORTNUMER = 2003;
const int NICK_LENGHT = 20;
//etc.


#ifdet __SERVER
//for other server modules
extern int iUsers;
//etc.
#endif
<cut>

than i added in makefile : -D__SERVER

all works good but i'm interested what are your opinions about this
tehnique and how you solve similar problems ? is it good idea to keep
two kinds of different information in one header file ? it allow to
gather all stuff in one place. maybe there are some better styles,
maybe you create a few different header files ... pls answer.
thanks in advance.
Kevin
 
J

Jack Klein

Hi there,
I've almost finished writing simple server application and i have one
little doubt. Suppose we have main server code in server.c file. Rest
of the code we divided into some pices and put into server_2.c,
server_3.c etc. Now we need to share some of the server.c variables
between server_2.c, server_3.c etc. so we put the declaractions of
these variables in server.h with "extern" prefix and include it
(server.h) in rest of the sources. We also put in server.h some consts
describing server, such a port number, version, max lenght of nick
etc. these information are necessary when someone would write a client
application. so in server.h we gathered two types of information :
external declarations for server sources (modules) and const for
future client developers. i used tehnique shown below to separate
these two kinds of adaptation:

in server.h

<cut>
//informations about server
const int SERVER_PORTNUMER = 2003;
const int NICK_LENGHT = 20;
//etc.

If you are using C, including this file will in more than one
translation unit (loosely, source code file) will result in undefined
behavior and most likely linker complaints about multiple definitions
of these integers with external linkage.

If you are using C++, you are asking in the wrong group.
#ifdet __SERVER

Even assuming you meant "#ifdef" instead of "#ifdet", this line is
illegal in both C and C++. Both languages prohibit the use of
identifiers beginning with two underscores, or an underscore followed
by an upper case letter.

Too many programmers copy this format for symbols from what they see
in header files that come with their compilers, without understanding
why compilers use symbols like this. The reason is that they are
specifically reserved for the implementation (compiler and its
headers) and will not conflict with any symbol you can legally define
in your program.
//for other server modules
extern int iUsers;
//etc.
#endif
<cut>

than i added in makefile : -D__SERVER

Make files and compiler command line options (like "-D") are off-topic
here, they are not language issues.
all works good but i'm interested what are your opinions about this
tehnique and how you solve similar problems ? is it good idea to keep
two kinds of different information in one header file ? it allow to
gather all stuff in one place. maybe there are some better styles,
maybe you create a few different header files ... pls answer.
thanks in advance.
Kevin

Consider changing the "const int" objects with external linkage to
either #define'd macros or enumeration values, and fix the illegal
symbols.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
K

Kevin Joplin

Hi there,
i should correct my mistakes:
i mean #ifdef of course ... so lets say
#ifdef SERVER
Consider changing the "const int" objects with external linkage to
either #define'd macros or enumeration values, and fix the illegal
symbols.

yes i have thare #define macros and enumeration values, sorry for such
big mistakes ...

could sb answer my questions now? thanks a lot.
Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top