473,580 Members | 2,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inlines with external linkage

Hi,

I have the following code:

/*************** *************** ** file1.c
#include <iostream>

extern void dummy();

inline int testfunc() {
return 1;
}

int main(int argc, char** argv) {
std::cout << testfunc();
dummy();

return 0;
}
/*************** *************** **********

/*************** *************** ** file2.c
#include <iostream>

inline int testfunc() {
return 2;
}

void dummy() {
std::cout << testfunc();
}
/*************** *************** **********

Now, I wasn't sure exactly what this code should do (by 'this code', I
mean the program obtained by compiling these two files and then linking
their respective object files). I was thinking it would output 12 or
there'd be an error of some description.

However, depending on the order of linkage, it succeeds and outputs 11
or 22. In other words, the linker is somehow removing multiple
definitions of this inline function and resolving both 'calls' to one of
them... but surely if it's inline, it will be dealt with at compile time
and the linker should have nothing to do with it? I know that 'inline'
doesn't guarantee inlining, but surely the linker can't decide not to
inline it, since surely that decision must be made at compile time. It
only behaves as expected if I force internal linkage on the testfuncs
with the 'static' keyword. There's something going on, because the
linker obviously 'doesn't mind' that this function has been multiply
defined, which I assume is because the compiler needed to have a
definition in each translation unit in order to be inlined it. I'm
confused though how the linker is resolving these multiple definitions
though. Any insight into inlines and external linkage etc. would be
greatly appreciated.

FYI, I'm using g++.

Thanks,

--

Richard Hayden
http://www.dx-dev.com
Jul 22 '05 #1
47 3827
Richard Hayden wrote:
SNIPPED


OK, I notice I mucked up the comments there, please ignore the fact that
there's no terminating */'s.

--

Richard Hayden
http://www.dx-dev.com
Jul 22 '05 #2
* Richard Hayden:

/*************** *************** ** file1.c
#include <iostream>

extern void dummy();

inline int testfunc() {
return 1;
}

int main(int argc, char** argv) {
std::cout << testfunc();
dummy();

return 0;
}
/*************** *************** **********

/*************** *************** ** file2.c
#include <iostream>

inline int testfunc() {
return 2;
}

void dummy() {
std::cout << testfunc();
}
/*************** *************** **********


You have Undefined Behavior (TM) because you have two different
definitions of the same external linkage 'inline' function.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3

"Richard Hayden" <ra***@doc.ic.a c.uk> wrote in message
news:cb******** *@sparta.btinte rnet.com...
Hi,

I have the following code:

/*************** *************** ** file1.c
#include <iostream>

extern void dummy();

inline int testfunc() {
return 1;
}

int main(int argc, char** argv) {
std::cout << testfunc();
dummy();

return 0;
}
/*************** *************** **********

/*************** *************** ** file2.c
#include <iostream>

inline int testfunc() {
return 2;
}

void dummy() {
std::cout << testfunc();
}
/*************** *************** **********

Now, I wasn't sure exactly what this code should do (by 'this code', I
mean the program obtained by compiling these two files and then linking
their respective object files). I was thinking it would output 12 or
there'd be an error of some description.

However, depending on the order of linkage, it succeeds and outputs 11
or 22.


Undefined behaviour as per 3.2/5.

The One Definition Rule allows you to repeat the definition of an inline
function as long as:
1) They appear in different translation units.
2) The are token-for-token identical and the meanings of these tokens
should be the same in both translation units.
Jul 22 '05 #4
Alf P. Steinbach wrote:
* Richard Hayden:
/*************** *************** ** file1.c
#include <iostream>

extern void dummy();

inline int testfunc() {
return 1;
}

int main(int argc, char** argv) {
std::cout << testfunc();
dummy();

return 0;
}
/*************** *************** **********

/*************** *************** ** file2.c
#include <iostream>

inline int testfunc() {
return 2;
}

void dummy() {
std::cout << testfunc();
}
/*************** *************** **********

You have Undefined Behavior (TM) because you have two different
definitions of the same external linkage 'inline' function.


Hi,

Thanks for your reply.

I understand what you mean in that case, so I have amended my code to:

/*************** ****test1.c**** ***************/
#include <iostream>

extern void dummy();

inline int testfunc() {
return 1;
}

int main(int argc, char** argv) {
std::cout << testfunc();
dummy();

return 0;
}
/*************** *************** ***************/

/*************** ****test2.c**** ***************/
#include <iostream>

extern int testfunc();

void dummy() {
std::cout << testfunc();
}
/*************** *************** ***************/

Surprisingly, it works as expected, i.e. the output is 11. How is this
occuring? Surely the linker must be doing some of the compiler's work
here in order to inline testfunc in the call in test2.c?

Thanks,

--

Richard Hayden
http://www.dx-dev.com
Jul 22 '05 #5

inline

is an abbreviation of:

static inline

-JKop
Jul 22 '05 #6
JKop wrote:
inline

is an abbreviation of:

static inline

-JKop


Really? I didn't think this was the case anymore.

--

Richard Hayden
http://www.dx-dev.com
Jul 22 '05 #7
* Richard Hayden:

* Alf P. Steinbach wrote:
You have Undefined Behavior (TM) because you have two different
definitions of the same external linkage 'inline' function.

I understand what you mean in that case, so I have amended my code to:

/*************** ****test1.c**** ***************/
#include <iostream>

extern void dummy();

inline int testfunc() {
return 1;
}

int main(int argc, char** argv) {
std::cout << testfunc();
dummy();

return 0;
}
/*************** *************** ***************/

/*************** ****test2.c**** ***************/
#include <iostream>

extern int testfunc();

void dummy() {
std::cout << testfunc();
}
/*************** *************** ***************/


Well this is simply invalid code. But it's fine details; I was
surprised when I saw the combination of 'inline' one place and
'extern' another place and had to look it up. §3.3/3: "An inline
function shall be defined in every translation unit in which it
is used", which is repeated in §7.1.2/4 which continues: "and
shall have exactly the same definition in every case. ... If a
function with external linkage is declared inline in one
translation unit, it shall be declared inline in all translation
units in which it appears; no diagnostic is required".
^^^^^^^^^^^^^^^ ^^^^^^^^^^

The extern'ness of inline functions is simply that they are (or can
be) visible to the linker so the linker can remove all but one
occurrence -- and you don't know which one, so must be identical.

Surprisingly, it works as expected, i.e. the output is 11.
Still UB...
How is this
occuring? Surely the linker must be doing some of the compiler's work
here in order to inline testfunc in the call in test2.c?


See above. But with your code it isn't required to. Because it's UB.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #8
* Richard Hayden:
JKop wrote:
inline

is an abbreviation of:

static inline

-JKop


Really? I didn't think this was the case anymore.


It isn't, and AFAIK never was.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #9
Alf P. Steinbach wrote:
[SNIPPED]


Many thanks for your help, I now believe I understand.

Kind Regards,

--

Richard Hayden
http://www.dx-dev.com
Jul 22 '05 #10

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

Similar topics

4
2440
by: Tron Thomas | last post by:
I have read that anonymous namespaces are preferred in C++ over declaring global statics because the namespace can hide information from other translation units and at the same time provide external linkage allowing members of the namespace to be used as template arguments. I'm not quite sure I'm clear on what this really means. What is an...
9
4192
by: maxw_cc | last post by:
Hi to all, To explain my question I'll help myself with the following two code snippets: /*** file1.c ***/ #include <stdio.h> void print_it(void);
19
2882
by: J. J. Farrell | last post by:
After many years of dealing with definition and linkage issues in ways that I know to be safe, I've decided it's time to try to understand this area properly. Consider a header file with the file scope declaration int i; This header is included in two files that refer to i but do not declare it. The two files build together into a single...
4
2729
by: Peter Ammon | last post by:
I would like to share a variable between two functions defined in two separate source files; no other functions will need the global variable so I'd prefer to not give it file scope. Thus, I want a variable with local scope, external linkage, and static storage. I believe I can do this for the file that does not define the variable by...
3
6017
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...
1
1510
by: Kurt | last post by:
The C++ standard (in 3.5:6, p42Example) said: static int i = 0; // 1 void g() { int i; //2: i has no linkage { extern int i; //3: external linkage
12
2237
by: destination | last post by:
"If a declaration for an identifier within a block does not include the extern specifier, then the identifier has no linkage and is unique to the function. If it does include extern, and an external declaration for is active in the scope surrounding the block, then the identifier has the same linkage as the external declaration, and refers to the...
0
8132
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. ...
0
8296
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...
0
8157
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...
0
6533
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...
1
5665
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...
0
3790
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...
0
3806
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1394
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.