Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > question about header files

Reply
Thread Tools

question about header files

 
 
Kevin Joplin
Guest
Posts: n/a
 
      07-02-2003
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
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      07-03-2003
On Wed, 02 Jul 2003 23:23:37 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
 
Kevin Joplin
Guest
Posts: n/a
 
      07-03-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
UNIX header files to Windows header files anand.ba@gmail.com C Programming 3 05-01-2006 03:57 PM
Header files included in header files John Smith C Programming 18 07-24-2004 04:55 AM
What is better /standard for creating files. a cpp file with header or cpp and seperate file for header DrUg13 C++ 1 02-10-2004 09:20 AM
how to avoid using another header file inside a header file? Newsgroup - Ann C++ 4 11-02-2003 01:20 PM



Advertisments