By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,445 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,445 IT Pros & Developers. It's quick & easy.

question about header files

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Wed, 02 Jul 2003 23:23:37 GMT, jo****@go2.pl (Kevin Joplin) wrote
in comp.lang.c:
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
Nov 13 '05 #2

P: n/a
Hi there,
i should correct my mistakes:
#ifdet __SERVER
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
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.