doubt on kinds of linkage

C

Carlos Martinez

Hi all:

I have old pieces of code like this:

//distsgip.cc
const RWCString var1;

//ReceptorDistIP.cc
extern const RWCString var1;

I don't like to use extern, but it is not my code.

I have a linkage error with var1 (undefined symbol)

If I remove const in both cases, code compiles and links ok.

¿Have a different linkage const and not const variables?

¿How can I solve the problem mantaining const?

Thanks in advance.
 
D

David Harmon

On Tue, 12 Sep 2006 11:52:09 +0200 in comp.lang.c++, Carlos Martinez
I have old pieces of code like this:

//distsgip.cc
const RWCString var1;

//ReceptorDistIP.cc
extern const RWCString var1;

I don't like to use extern, but it is not my code.

There is nothing wrong with extern!
I have a linkage error with var1 (undefined symbol)


//distsgip.cc
extern const RWCString var1;
const RWCString var1;

Yes, it's true. Put both lines in.

Or put the "extern" declaration in a header and include it in both
distsgip.cc and ReceptorDistIP.cc if you want to go that way.
 
M

Morten V Pedersen

Hi,
A const can be given external linkage by an explicit declaration:

In your case it should be:

//distsgip.cc
extern const RWCString var1;

//ReceptorDistIP.cc
extern const RWCString var1;

// morten
 
D

David Harmon

On Tue, 12 Sep 2006 14:48:22 +0200 in comp.lang.c++, Morten V
Pedersen said:
Hi,
A const can be given external linkage by an explicit declaration:

In your case it should be:

//distsgip.cc
extern const RWCString var1;

//ReceptorDistIP.cc
extern const RWCString var1;

// morten

But of course if that is all you have then var1 will end up as an
"unresolved extern" at link time! You need the other part too.
 
B

Bart

Carlos said:
I have old pieces of code like this:

//distsgip.cc
const RWCString var1;

//ReceptorDistIP.cc
extern const RWCString var1;

I don't like to use extern, but it is not my code.

I have a linkage error with var1 (undefined symbol)

Of course, it's a constant so you need to initialize it somewhere. In
one of the files you must have:

const RWCString var1 = <constant expression>;

Regards,
Bart.
 
D

David Harmon

On 12 Sep 2006 12:37:36 -0700 in comp.lang.c++, "Bart"
Of course, it's a constant so you need to initialize it somewhere. In
one of the files you must have:

const RWCString var1 = <constant expression>;

No, the class default constructor may be all you need to initialize
that particular instance. We have no idea what the default
constructor says. I do agree that it would be wise to be a bit
suspicious of the lack of initialization parameters, but it is
perhaps only Carlos's simplified example for purposes of the
question.
 

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
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top