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