473,386 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Const symbols suppressed

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
Jan 18 '08 #1
2 1688
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
Jan 18 '08 #2
On Fri, 18 Jan 2008 08:38:37 -0800 (PST), PlayDough <pl****@gmail.com>
wrote in comp.lang.c++:
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
Jan 19 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is...
2
by: John Stiles | last post by:
I have written some pretty simple code which works great in Dev Studio and CodeWarrior, but gcc is giving me link errors. Here is what I've been able to distill it down to: namespace Private {...
47
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols...
6
by: Alvin | last post by:
Hello, I have a static library that I created. I've been updating the source to be const-correct as according to the C++ FAQ Lite section 18...
9
by: C. J. Clegg | last post by:
When you say "const int FOO = 0" (as the commonly-recommended C++ alternative to "#define FOO 0"), isn't that declaration globally visible? I have "const int FOO = 0;" in one source file and...
13
by: Michal Nazarewicz | last post by:
The following code compiles fine (with warnings though) under GCC with -pedantic and -ansi arguments: #v+ int main(void) { const int a = 0; int *p = &a; return 0; } #v-
20
by: subramanian100in | last post by:
Suppose I have #included <stdint.h> #define MAX_LINE_SIZE SIZE_MAX or const size_t maxLineLength = SIZE_MAX ; Between the above #define and const declaration, which should be...
4
by: newbarker | last post by:
Hello, Very basic question here. If I have the following constants declared in a.cpp and in b.cpp, would there be any possibility of duplicate symbols?: a.cpp -------- #include <string>
21
by: Christian Meier | last post by:
Hello NG I have the following code: file1.h: static const int iValue = 5; <EOF>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.