Const symbols suppressed

P

PlayDough

I think this is something about C++ that I don't understand, and in
versions of g++ prior to 4.1.1, hasn't been a problem.

Say I have this basic file:

test.cpp:
const char test_str[] = "abcd";

And I compile this. I'd expect test_str to be exported so other
modules can use it. So, I do nm (for GNU) and dumpbin (for Visual
Studio), and get....nothing.

The test_str is not exported. In fact, dumping the output (objdump
and dumpbin) shows nothing. And dumping the headers no readonly data
section (.rodata for GNU, .rdata for VS). No output at all.

But change things up. Change the filename to test.c and recompile.
Now test_str is exported, and is placed in the .rodata/.rdata
section. Exactly as expected.

The only fix I have found for this is to do one of two things.

test.cpp:
extern const char test_str[];
const char test_str[] = "abcd";

or

test.cpp:
extern const char test_str[] = "abcd";

I dislike the latter case, since we only use extern with declarations,
and leave off the extern for allocations. That is, we'll put the
extern statement in a header, and leave off the extern in the source
file.

So, only with the extern keyword does the symbol get exported.

Is this a C++ feature that has recently been implemented in the GNU
tools? We frequently have been doing something of the sort:

test.h:
extern const char test_str[];

void some_func(int, int);

test.cpp:
const char test_str[];

void some_func(int x, int y)
{
...
}

Other modules include test.h and can now reference test_str. And we
normally don't include test.h into test.cpp because it isn't
necesssary all of the time (though not a bad idea, especially if you
want to verify prototypes match the functions). But now it appears
necessary.

Can somebody clarify thie functionality?

Thanks,
Pete
 
P

PlayDough

I finally figured out the proper search terms so that I could find the
answer to this question.

Now I understand that const symbols, by default, have internal
linkage, unlike C. And apparently, g++ was broken prior to 4.1.1.

Sorry to clutter things.

Pete
 
J

Jack Klein

I finally figured out the proper search terms so that I could find the
answer to this question.

Now I understand that const symbols, by default, have internal
linkage, unlike C. And apparently, g++ was broken prior to 4.1.1.

Not broken, just omitting a permissible, but not required,
optimization.

Since it has internal linkage, it can only be referred to in the
current translation unit. Other translation units could refer to the
char array if code in the current TU passed or returned a pointer to
the start or some other member of the array, but to do this there
would need to be an expression that referenced the array name to
create such a pointer.

If the compiler sees that the current TU does not reference an object
with internal linkage, including a reference to create a pointer to
it, it knows that the array cannot actually be accessed by the program
at all.

Some C compilers will do the same thing if the const char array is
defined with the static keyword as well.
Sorry to clutter things.

No problem, it is one of the decisions that creates a relatively
silent change from C in C++. Some regard it as an unfortunate choice,
others agree like it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top