473,770 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1710
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.c om>
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.l earn.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
3002
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 very funny. eScrew story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing
2
1977
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 { template <class T> struct lengthof; template <class T, size_t N> struct lengthof<T> { static const size_t array_size = N; }; }
47
3368
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 are objects whose representation within the code is more important than their actual value. Two symbols needs only to be
6
354
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 (http://www.parashift.com/c++-faq-lite/const-correctness.html). I know I should have done this in the first place, but better late than never. ;) I have been declaring inspect functions const as in: int inspect() const;
9
5119
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 "extern int FOO;" in another source file (also tried "extern const int FOO;") and at link time, I get undefined references to FOO. Removed the "const" from the FOO declaration and everything worked fine.
13
1484
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
5565
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 preferred and why ?
4
1615
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
2763
by: Christian Meier | last post by:
Hello NG I have the following code: file1.h: static const int iValue = 5; <EOF>
0
9453
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.