Definition in Header files - Unix

This is a discussion on Definition in Header files - Unix ; I have problems faced in adding definitions in the command header file. I have defined a header file which includes huge set of global constants and I am using them in all the C files. For example my sample.h file ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: Definition in Header files

  1. Definition in Header files

    I have problems faced in adding definitions in the command header
    file. I have defined a header file which includes huge set of global
    constants and I am using them in all the C files.

    For example my sample.h file looks like this

    /*************

    File : sample.h

    ****************/


    const int a1 = 0;
    const int a2 = 0;


    /****End of sample.h*********/


    My source code looks like this

    /*****************
    File:sample1.c

    ***********/
    #include "sample.h"

    int main()
    {
    }

    /*****End of sample1.c************/

    /***************
    File : sample2.c
    **************/

    #include "sample.h

    int main()
    {
    }

    /*******End of sample2.c**********/

    Now the problem is during linking it says multiple definition of
    symbols a1 and a2. I can avoid this problem by having a C file which
    contains the definitions and H file for declarations. But is there any
    way I can avoid doing this. Please let me know.


  2. Re: Definition in Header files

    On Mon, 22 Oct 2007 22:40:22 -0700 Madhur wrote:
    > I have problems faced in adding definitions in the command header
    > file. I have defined a header file which includes huge set of global
    > constants and I am using them in all the C files.

    ....
    > Now the problem is during linking it says multiple definition of
    > symbols a1 and a2. I can avoid this problem by having a C file which
    > contains the definitions and H file for declarations. But is there any
    > way I can avoid doing this. Please let me know.


    No.

    But why do you want to avoid it? Maybe we can suggest a better approach.

  3. Re: Definition in Header files

    Madhur writes:

    > I have problems faced in adding definitions in the command header
    > file. I have defined a header file which includes huge set of global
    > constants and I am using them in all the C files.
    >
    > For example my sample.h file looks like this
    >
    > /*************
    >
    > File : sample.h
    >
    > ****************/
    >
    >
    > const int a1 = 0;
    > const int a2 = 0;
    >
    >
    > /****End of sample.h*********/
    >
    >
    > My source code looks like this
    >
    > /*****************
    > File:sample1.c
    >
    > ***********/
    > #include "sample.h"
    >
    > int main()
    > {
    > }
    >
    > /*****End of sample1.c************/
    >
    > /***************
    > File : sample2.c
    > **************/
    >
    > #include "sample.h
    >
    > int main()
    > {
    > }
    >
    > /*******End of sample2.c**********/
    >
    > Now the problem is during linking it says multiple definition of
    > symbols a1 and a2. I can avoid this problem by having a C file which
    > contains the definitions and H file for declarations. But is there any
    > way I can avoid doing this. Please let me know.


    I will repeat the "why avoid the obvious way" advice, but add a
    "solution". In comp.lang.c you asked why this is not the same in C
    and C++. In C++ const definitions that are not explicitly marked
    extern have internal linkages. I.e. you are getting (in C++) two
    copies of variable (OK, the linked may elide them into one, but that
    is a matter outside the standard).

    Mark the definitions "static const" and C behaves much like you seem
    to expect.

    --
    Ben.

  4. Re: Definition in Header files

    On Tue, 23 Oct 2007 16:01:10 +0100 Ben Bacarisse wrote:
    > Madhur writes:
    >
    >> I have problems faced in adding definitions in the command header
    >> file. I have defined a header file which includes huge set of
    >> global constants and I am using them in all the C files.

    ^^^^^^^^^^^^^^^^

    ....
    >> Now the problem is during linking it says multiple definition of
    >> symbols a1 and a2. I can avoid this problem by having a C file which
    >> contains the definitions and H file for declarations. But is there any
    >> way I can avoid doing this. Please let me know.

    >
    > I will repeat the "why avoid the obvious way" advice, but add a
    > "solution". In comp.lang.c you asked why this is not the same in C
    > and C++. In C++ const definitions that are not explicitly marked
    > extern have internal linkages. I.e. you are getting (in C++) two
    > copies of variable (OK, the linked may elide them into one, but that
    > is a matter outside the standard).
    >
    > Mark the definitions "static const" and C behaves much like you seem
    > to expect.


    Not at all. They lose the global [across all modules] property, which
    is what I presume to be the intent.

    -frank

  5. Re: Definition in Header files

    Frank Cusack writes:

    > On Tue, 23 Oct 2007 16:01:10 +0100 Ben Bacarisse wrote:
    >> Madhur writes:
    >>
    >>> I have problems faced in adding definitions in the command header
    >>> file. I have defined a header file which includes huge set of
    >>> global constants and I am using them in all the C files.

    > ^^^^^^^^^^^^^^^^
    >
    > ...
    >>> Now the problem is during linking it says multiple definition of
    >>> symbols a1 and a2. I can avoid this problem by having a C file which
    >>> contains the definitions and H file for declarations. But is there any
    >>> way I can avoid doing this. Please let me know.

    >>
    >> I will repeat the "why avoid the obvious way" advice, but add a
    >> "solution". In comp.lang.c you asked why this is not the same in C
    >> and C++. In C++ const definitions that are not explicitly marked
    >> extern have internal linkages. I.e. you are getting (in C++) two
    >> copies of variable (OK, the linked may elide them into one, but that
    >> is a matter outside the standard).
    >>
    >> Mark the definitions "static const" and C behaves much like you seem
    >> to expect.

    >
    > Not at all. They lose the global [across all modules] property, which
    > is what I presume to be the intent.


    Elsewhere the OP complained that the code worked in C++ but not in C.
    In C++, file-scope const definitions have internal linkage, just like
    static file-scope definitions in C, hence my odd suggestion.

    It is hard to tell what is really intended since the original post
    sported code with multiple main functions defined which makes no sense
    in either language.

    Because they probably do want "global constants" I repeated the
    advice already given: "why avoid the obvious way?".

    --
    Ben.

  6. Re: Definition in Header files

    In article ,
    Frank Cusack wrote:

    > On Tue, 23 Oct 2007 16:01:10 +0100 Ben Bacarisse wrote:
    > > Madhur writes:
    > >
    > >> I have problems faced in adding definitions in the command header
    > >> file. I have defined a header file which includes huge set of
    > >> global constants and I am using them in all the C files.

    > ^^^^^^^^^^^^^^^^
    >
    > ...
    > >> Now the problem is during linking it says multiple definition of
    > >> symbols a1 and a2. I can avoid this problem by having a C file which
    > >> contains the definitions and H file for declarations. But is there any
    > >> way I can avoid doing this. Please let me know.

    > >
    > > I will repeat the "why avoid the obvious way" advice, but add a
    > > "solution". In comp.lang.c you asked why this is not the same in C
    > > and C++. In C++ const definitions that are not explicitly marked
    > > extern have internal linkages. I.e. you are getting (in C++) two
    > > copies of variable (OK, the linked may elide them into one, but that
    > > is a matter outside the standard).
    > >
    > > Mark the definitions "static const" and C behaves much like you seem
    > > to expect.

    >
    > Not at all. They lose the global [across all modules] property, which
    > is what I presume to be the intent.


    Since they're just constants, what difference does it make whether
    they're global or local?

    The only way you'd be able to tell is if you compare addresses.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  7. Re: Definition in Header files

    On Oct 23, 1:40 am, Madhur wrote:
    > I have problems faced in adding definitions in the command header
    > file. I have defined a header file which includes huge set of global
    > constants and I am using them in all the C files.
    >
    > For example my sample.h file looks like this
    >
    > /*************
    >
    > File : sample.h
    >
    > ****************/
    >
    > const int a1 = 0;
    > const int a2 = 0;
    >
    > /****End of sample.h*********/
    >
    > My source code looks like this
    >
    > /*****************
    > File:sample1.c
    >
    > ***********/
    > #include "sample.h"
    >
    > int main()
    > {
    >
    > }
    >
    > /*****End of sample1.c************/
    >
    > /***************
    > File : sample2.c
    > **************/
    >
    > #include "sample.h
    >
    > int main()
    > {
    >
    > }
    >
    > /*******End of sample2.c**********/
    >
    > Now the problem is during linking it says multiple definition of
    > symbols a1 and a2. I can avoid this problem by having a C file which
    > contains the definitions and H file for declarations. But is there any
    > way I can avoid doing this. Please let me know.


    Can you do something like the following?

    jc@jc-ubuntu:~/tmp/externtest$ cat sample.h
    #ifdef ALLOC_GLOBALS
    # define STCLASS extern
    # define I(x)
    #else
    # define STCLASS
    # define I(x) x
    #endif

    STCLASS const int a1 I(=0) ;
    STCLASS const int a2 I(=0) ;

    jc@jc-ubuntu:~/tmp/externtest$ cat sample1.c
    #define ALLOC_GLOBALS
    #include "sample.h"

    int main() {
    }

    jc@jc-ubuntu:~/tmp/externtest$ cat sample2.c
    #include "sample.h"

    int main_2() {
    }


+ Reply to Thread