question about header files

Discussion in 'C Programming' started by Kevin Joplin, Jul 3, 2003.

  1. Kevin Joplin

    Kevin Joplin Guest

    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
    Kevin Joplin, Jul 3, 2003
    #1
    1. Advertising

  2. Kevin Joplin

    Jack Klein Guest

    On Wed, 02 Jul 2003 23:23:37 GMT, (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
    Jack Klein, Jul 3, 2003
    #2
    1. Advertising

  3. Kevin Joplin

    Kevin Joplin Guest

    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
    Kevin Joplin, Jul 3, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    4
    Views:
    692
    lilburne
    Nov 2, 2003
  2. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    596
    Jack Klein
    Jul 24, 2004
  3. Replies:
    3
    Views:
    1,282
    Walter Roberson
    May 1, 2006
  4. Bit byte
    Replies:
    1
    Views:
    640
    benben
    Apr 19, 2006
  5. mlt
    Replies:
    2
    Views:
    820
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page