constant global variables using in other compilation units,

R

Rahul

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!!!
 
S

sks

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.
 
R

Rahul

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!!!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top