Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > constant global variables using in other compilation units,

Reply
Thread Tools

constant global variables using in other compilation units,

 
 
Rahul
Guest
Posts: n/a
 
      01-07-2008
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!!!
 
Reply With Quote
 
 
 
 
sks
Guest
Posts: n/a
 
      01-07-2008
On Jan 7, 12:56*pm, Rahul <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
 
Rahul
Guest
Posts: n/a
 
      01-07-2008
On Jan 7, 1:49 pm, sks <(E-Mail Removed)> wrote:
> On Jan 7, 12:56 pm, Rahul <(E-Mail Removed)> 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!!!
 
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
concatenate a constant to constant string using macros sinbad C Programming 7 06-19-2008 05:28 PM
Initialize global variable before any other global variables jubelbrus C++ 5 07-20-2007 06:38 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Help with Javascript: naming variables using other variables sagejoshua HTML 16 11-09-2005 12:27 PM



Advertisments