C++ linkage problems

W

Web Developer

Hi,

I read that identifiers should not start with an underscore in order to
prevent LINKAGE problems. I am not familar with this concept of "linkage",
so can someone provide a simple explanation?

Thanks in advance

WD
 
G

Greg Comeau

I read that identifiers should not start with an underscore in order to
prevent LINKAGE problems.

The strongest argument is that there are rules in the standard
that in a nushell say such names are only available for implementors,
in other words, for your compiler or library vendor.
I am not familar with this concept of "linkage",
so can someone provide a simple explanation?

Generally speaking, linkage applies to names of entities and
is one mechanism whereby implementations can disabmiguate their use.
In particular, when it needs to resolve where to uses of a name
refers to the same name. For instance, in external linkage,
the use of a "global" name in one translation unit (say a source file)
is considered to be the same name used in another translation
unit that will be linked together. It ties into scope and some
other things too. There is also static linkage and no linkage.
You should get a good C++ text and check out the section that
apply to this topics you raise in this email. Check out
http://www.comeaucomputing.com/booklist as one starting point.
 
T

Thomas Matthews

Greg said:
I think you mean trailing?

No, I meant leading. A C++ compliant compiler will not generate
an error for the following line:
int __my_var;
provided that the defined variable doesn't exist as a public
symbol in the compiler's internals.

However, there may be a problem with the following definition:
int __FILE__;
Because __FILE__ is used by the compiler (actually the preprocessor).

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
G

Greg Comeau

No, I meant leading. A C++ compliant compiler will not generate
an error for the following line:
int __my_var;
provided that the defined variable doesn't exist as a public
symbol in the compiler's internals.

2.10 and 17.4.3.1.2 clearly label this as a restriction.
Of course, certain "errors" need not result in a diagnostic,
but that's a seperate issue.
However, there may be a problem with the following definition:
int __FILE__;

Agreed. See above.
Because __FILE__ is used by the compiler

s/used/may be used/

Because __FILE__ begins with two underscores, and Standard C++
reads "Each name that contains a double underscore (_ _) ...
is reserved to the implementation for any use."
(actually the preprocessor).

Perhaps.
 
W

Web Developer

Symbols starting with an underscore are reserved for the compiler or
implementation. A programmer's symbol should not start with an
underscore to prevent name clashes or conflicts with compiler symbols.


What do you mean by implementation?


WD
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top