473,790 Members | 2,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Doubts about Linkage Rules

source: http://rm-f.net/~orange/devel/specif...t.html#3.1.2.2

there are two passages in this paragraph i can't fully understand:

1) "If the declaration of an identifier for an object or a function
contains the storage-class specifier extern , the identifier has the
same linkage as any visible declaration of the identifier with file
scope. If there is no visible declaration with file scope, the
identifier has external linkage."

in particular: "the identifier has the same linkage as /any/ /visible/
declaration of the identifier with file scope"

2) "If, within a translation unit, the same identifier appears with both
internal and external linkage, the behavior is undefined."

i can't imagine an example of an identifier having both internal and
external linkage...
Apr 17 '06
13 2097

fctk wrote:
Vladimir S. Oka ha scritto:
Maybe you should try thinking about this particular example in this
way:

static int bar;

Tells the compiler that `bar` is "visible" only internally to the file
in question.

int bar;

Then tells the compiler that it is "visible" externally as well.

Logically, both can't be true at the same time, hence the error.


mmh... but i think there is another error. `static int bar' and `int
bar' are two *definitions* other than two declarations. they are two
definitions of the *same* object.

it is as if i wrote:

static void f1(void) {}
extern void f1(void) {}

isn't it?


Yes, that too. I was just pointing out the other discrepancy.

Apr 18 '06 #11
Diomidis Spinellis ha scritto:
It means that the following is a legal declaration of foo with file
linkage scope:

static int foo;
extern int foo;

[...]

Note that C99 clarifies the *visible* part of the specification as
follows (6.2.2):

"For an identifier declared with the storage-class specifier extern in a
scope in which a *prior* declaration of that identifier is visible, if
the prior declaration specifies internal or external linkage, the
linkage of the identifier at the later declaration is the same as the
linkage specified at the prior declaration. If no prior declaration is
visible, or if the prior declaration specifies no linkage, then the
identifier has external linkage."


sorry but i don't understand.

let me rewrite that example as follows:

static int foo; /* D1 */
/* S1 */
/* S1 */ extern int foo; /* D2 */
/* S1 */ /* S2 */

/* D1 */ : declaration1; first declaration of identifier `foo'
/* D2 */ : declaration2; second declaration of identifier `foo'
/* S1 */ : scope1; scope of the first declaration
/* S2 */ : scope2; scope of the second declaration

i don't understand this passage of the rule: "for an identifier declared
[...] in a scope in which a *prior* declaration of that identifier /is/
/visible/, [...]".

D2 is inside S1. let suppose S1 is "visible" to D2. the problem is D1 is
*not* inside S1, so D2 does not know the existence of D1.

this problem could be resolved by "extending" the scopes in the
following way:

/* S1 */static int foo; /* D1 */
/* S1 */
/* S1 */ /* S2 */ extern int foo; /* D2 */
/* S1 */ /* S2 */

all `foo's would have interal-linkage. (( question: but do the instance
of an identifier which occour in a declaration of that identifier have a
kind of linkage? ))

anyway, this "extension" would contract with this other rule:

"Structure, union, and enumeration tags have scope that begins [...].
Each enumeration constant has scope that begins [...]. Any other
identifier has scope that begins /just/ /after/ the completion of its
declarator."
Apr 18 '06 #12
Vladimir S. Oka wrote:
fctk wrote:
Vladimir S. Oka ha scritto:
Maybe you should try thinking about this particular example in this
way:

static int bar;

Tells the compiler that `bar` is "visible" only internally to the file
in question.

int bar;

Then tells the compiler that it is "visible" externally as well.

Logically, both can't be true at the same time, hence the error.

mmh... but i think there is another error. `static int bar' and `int
bar' are two *definitions* other than two declarations. they are two
definitions of the *same* object.

it is as if i wrote:

static void f1(void) {}
extern void f1(void) {}

isn't it?


Yes, that too. I was just pointing out the other discrepancy.


Actually no, they are both tentative definitions, where as you example
with functions shows definitions. For example, the following is
completely legal:

static int bar; /* tentative definition */
static int bar; /* tentative definition */
static int bar; /* tentative definition */
static int bar; /* tentative definition */
static int bar = 5; /* definition */

This defines only one object named bar which is of type int and is
initialised to 5.

From section 6.9.2 of n1124.pdf
| A declaration of an identifier for an object that has file scope
| without an initializer, and without a storage-class specifier or with
| the storage-class specifier static, constitutes a tentative
^^^^^^^^^
| definition. If a translation unit contains one or more tentative
^^^^^^^^^^
| definitions for an identifier, and the translation unit contains no
| external definition for that identifier, then the behavior is exactly
| as if the translation unit contains a file scope declaration of that
| identifier, with the composite type as of the end of the translation
| unit, with an initializer equal to 0.

Here is example 1 from the same section:
int i1 = 1; // definition, external linkage
static int i2 = 2; // definition, internal linkage
extern int i3 = 3; // definition, external linkage
int i4; // tentative definition, external linkage
static int i5; // tentative definition, internal linkage
int i1; // valid tentative definition, refers to pre vious
int i2; // 6.2.2 renders undefined, linkage disagreement
int i3; // valid tentative definition, refers to pre vious
int i4; // valid tentative definition, refers to pre vious
int i5; // 6.2.2 renders undefined, linkage disagreement
extern int i1; // refers to pre vious, whose linkage is external
extern int i2; // refers to pre vious, whose linkage is internal
extern int i3; // refers to pre vious, whose linkage is external
extern int i4; // refers to pre vious, whose linkage is external
extern int i5; // refers to pre vious, whose linkage is internal

Note that there are only two problem lines in the above example, delete
(or correct) those two lines and it would be completely legal C99 (if
not for the // style comments it would be legal C89 as well).
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Apr 18 '06 #13

Flash Gordon wrote:
Vladimir S. Oka wrote:
fctk wrote:
Vladimir S. Oka ha scritto:
Maybe you should try thinking about this particular example in this
way:

static int bar;

Tells the compiler that `bar` is "visible" only internally to the file
in question.

int bar;

Then tells the compiler that it is "visible" externally as well.

Logically, both can't be true at the same time, hence the error.

mmh... but i think there is another error. `static int bar' and `int
bar' are two *definitions* other than two declarations. they are two
definitions of the *same* object.

it is as if i wrote:

static void f1(void) {}
extern void f1(void) {}

isn't it?


Yes, that too. I was just pointing out the other discrepancy.


Actually no, they are both tentative definitions, where as you example
with functions shows definitions.


Right. I stand corrected on the variable part. Should've been more
careful.

After all, speed /is/ the device of shaitan.

Apr 18 '06 #14

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

Similar topics

5
2795
by: Web Developer | last post by:
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
9
12144
by: qazmlp | last post by:
const has internal linkage in C++, but external linkage in C. Am I right ? But, linker reports multiply-defined error if the following header is included in multiple .cpp files. // test_const.h #ifndef HEADER_TEST #define HEADER_TEST
47
3885
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
20
3157
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with extern linkage is inlined? Should the compiler still export the function? Or is an inlined function implicitly static?
10
6200
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
4
1132
by: project | last post by:
Anybody can solve following doubts? 1. Normalization rules. 2. Garbage Collection 3.LinkList Posted Via Usenet.com Premium Usenet Newsgroup Services ---------------------------------------------------------- ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY ** ----------------------------------------------------------
3
6038
by: al.cpwn | last post by:
do static and inline functions or members have internal linkage? I have been reading this newsgroup on google and found conflicting ideas. Can someone please help me understand why in some places inline may have external linkage while in others it has internal (from what I have read in comments by people). Are there any (other) places where linkage is ambiguous?
3
13258
by: Rahul Babbar | last post by:
Hi, I had the following doubts about the "For Read Only" clause. 1. How does a "for Read only" clause improve the performance? 2. How does a "for Read only" clause compare with "With UR" clause in performance? Which is faster? Can someone clarify on that?
1
3761
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: - "function prototype scope" for structures and unions? - "extern" for internal linkage ?
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
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
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
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
6769
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
5422
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
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2909
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.