constant global variables using in other compilation units,

Discussion in 'C++' started by Rahul, Jan 7, 2008.

  1. Rahul

    Rahul Guest

    Hi,

    Everyone i get the following error when i compile and link both of
    the files,
    : undefined reference to `a'

    a.c

    #include <cstdio>
    const int a = 6;
    extern int fun();
    int main()
    {
    printf("a in a is %d",a);
    fun();
    return ( 0 ) ;
    }


    b.c

    # include <cstdio>
    extern const int;
    int fun()
    {
    printf("a in b.c is %d\n",a);
    return ( 0 ) ;
    }



    However it works fine for the following cases, ( which i feel doesn't
    make sense, as a new value could be set to the constant varialbe)

    a.c

    #include <cstdio>
    const int a = 6;
    extern int fun();
    int main()
    {
    printf("a in a is %d",a);
    fun();
    return ( 0 ) ;
    }


    b.c

    # include <cstdio>
    extern const int a = 7;
    int fun()
    {
    printf("a in b.c is %d\n",a);
    return ( 0 ) ;
    }

    Thanks in advance!!!
    Rahul, Jan 7, 2008
    #1
    1. Advertising

  2. Rahul

    sks Guest

    On Jan 7, 12:56 pm, Rahul <> wrote:
    > Hi,
    >
    >  Everyone i get the following error when i compile and link both of
    > the files,
    >            : undefined reference to `a'
    >
    > a.c
    >
    > #include <cstdio>
    > const int a = 6;
    > extern int fun();
    > int main()
    > {
    >  printf("a in a is %d",a);
    >  fun();
    >  return ( 0 ) ;
    >
    > }
    >
    > b.c
    >
    > # include <cstdio>
    > extern const int;
    > int fun()
    > {
    >  printf("a in b.c is %d\n",a);
    >  return ( 0 ) ;
    >
    > }
    >
    > However it works fine for the following cases, ( which i feel doesn't
    > make sense, as a new value could be set to the constant varialbe)
    >
    > a.c
    >
    > #include <cstdio>
    > const int a = 6;
    > extern int fun();
    > int main()
    > {
    >  printf("a in a is %d",a);
    >  fun();
    >  return ( 0 ) ;
    >
    > }
    >
    > b.c
    >
    > # include <cstdio>
    > extern const int a = 7;
    > int fun()
    > {
    >  printf("a in b.c is %d\n",a);
    >  return ( 0 ) ;
    >
    > }
    >
    > Thanks in advance!!!



    use extern const int a = 6; in the a.c file

    according to ISO C++ rules, const definitions have internal
    linkage, so const int a= 6; is as if you define it static
    int a= 6; (or in an anonymous namespace). So if you want your const
    to have external linkage, you will have to use the keyword extern
    explicitly.
    sks, Jan 7, 2008
    #2
    1. Advertising

  3. Rahul

    Rahul Guest

    On Jan 7, 1:49 pm, sks <> wrote:
    > On Jan 7, 12:56 pm, Rahul <> wrote:
    >
    >
    >
    > > Hi,

    >
    > > Everyone i get the following error when i compile and link both of
    > > the files,
    > > : undefined reference to `a'

    >
    > > a.c

    >
    > > #include <cstdio>
    > > const int a = 6;
    > > extern int fun();
    > > int main()
    > > {
    > > printf("a in a is %d",a);
    > > fun();
    > > return ( 0 ) ;

    >
    > > }

    >
    > > b.c

    >
    > > # include <cstdio>
    > > extern const int;
    > > int fun()
    > > {
    > > printf("a in b.c is %d\n",a);
    > > return ( 0 ) ;

    >
    > > }

    >
    > > However it works fine for the following cases, ( which i feel doesn't
    > > make sense, as a new value could be set to the constant varialbe)

    >
    > > a.c

    >
    > > #include <cstdio>
    > > const int a = 6;
    > > extern int fun();
    > > int main()
    > > {
    > > printf("a in a is %d",a);
    > > fun();
    > > return ( 0 ) ;

    >
    > > }

    >
    > > b.c

    >
    > > # include <cstdio>
    > > extern const int a = 7;
    > > int fun()
    > > {
    > > printf("a in b.c is %d\n",a);
    > > return ( 0 ) ;

    >
    > > }

    >
    > > Thanks in advance!!!

    >
    > use extern const int a = 6; in the a.c file
    >
    > according to ISO C++ rules, const definitions have internal
    > linkage, so const int a= 6; is as if you define it static
    > int a= 6; (or in an anonymous namespace). So if you want your const
    > to have external linkage, you will have to use the keyword extern
    > explicitly.


    Thanks, i was just trying some combinations too,

    a.c b.c compilation and linking

    int a; int a; Multiple definition for a
    const int a = 6; int a; Ok
    int a; const int a = 66; Ok
    const int a = 6; const int a = 7 Ok (Constant's internal linkage)

    Just like first case, i expected a linker error for the second and
    third case too, but it works fine... i expected the global a to be
    visible in other compilation unit where it is declared as another
    constant, causing the linker to complain...

    Thanks in advance!!!
    Rahul, Jan 7, 2008
    #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. Spiros Bousbouras
    Replies:
    9
    Views:
    331
    David T. Ashley
    Dec 19, 2006
  2. Matthias Kaeppler
    Replies:
    3
    Views:
    268
    Matthias Kaeppler
    Sep 26, 2005
  3. =?ISO-8859-1?Q?Jens_M=FCller?=

    enum used in multiple compilation units

    =?ISO-8859-1?Q?Jens_M=FCller?=, Oct 25, 2006, in forum: C++
    Replies:
    3
    Views:
    380
    =?ISO-8859-1?Q?Jens_M=FCller?=
    Oct 25, 2006
  4. Replies:
    1
    Views:
    310
    Alf P. Steinbach
    Nov 27, 2006
  5. jubelbrus
    Replies:
    5
    Views:
    601
    JohnQ
    Jul 20, 2007
Loading...

Share This Page