具有外部链接的内联 [英] Inlines with external linkage

查看:69
本文介绍了具有外部链接的内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有以下代码:


/ ************** ****************** file1.c

#include< iostream>


extern void dummy ();


inline int testfunc(){

返回1;

}


int main(int argc,char ** argv){

std :: cout<< testfunc();

dummy();


返回0;

}

/ * *******************************************


/ ******************************** file2.c

#include< iostream> ;


inline int testfunc(){

返回2;

}


void dummy(){

std :: cout<< testfunc();

}

/ **************************** ************


现在,我不确定这段代码到底应该做什么(通过''this code'',我是/>
表示通过编译这两个文件然后链接

各自的目标文件获得的程序。我以为它会输出12或

会有一些描述错误。


但是,根据连接的顺序,它会成功并且输出11

或22.换句话说,链接器以某种方式删除了这个内联函数的多个

定义并解析了两个''调用''到其中一个/>
他们......但肯定如果它是内联的,它将在编译时处理

并且链接器应该与它无关?我知道''内联''

并不能保证内联,但肯定链接器不能决定不用b $ b b内联它,因为肯定这个决定必须在编译时制作。如果我使用''static''关键字强制testfuncs

上的内部链接,那么
只会按预期运行。还有一些事情正在发生,因为

链接器显然''并不介意'这个函数已经定义了多个

,我认为这是因为编译器需要在每个翻译单元中定义

定义才能将其内联。虽然链接器如何解析这些多个定义,但是我很困惑。[b
$ b}。任何有关内联和外部链接等的见解都将非常感激。


仅供参考,我正在使用g ++。

谢谢,


-


Richard Hayden
http://www.dx-dev.com

解决方案

Richard Hayden写道:

SNIPPED




好​​的,我注意到我把评论搞砸了,请忽略

那里没有终止* /'s。


-


Richard Hayden
http://www.dx-dev.com


* Richard Hayden:


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

extern void dummy();

inline int testfunc(){
返回1;
}

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

返回0;
}
/ ***************** ***********************

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

inline int testfunc(){
返回2;
}

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




您有Undefined Behavior(TM),因为您有两个不同的

定义相同的外部链接''inline' '功能。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?




" Richard Hayden" < RA *** @ doc.ic.ac.uk>在消息中写道

新闻:cb ********* @ sparta.btinternet.com ...



我有以下代码:

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

extern void dummy();

inline int testfunc(){
返回1;
}

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

返回0;
}
/ ***************** ***********************

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

inline int testfunc(){
返回2;
}

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

现在,我不确定这段代码到底应该做什么(通过''this code'',我/
意味着通过编译这两个文件得到的程序和然后链接各自的目标文件)。我认为它会输出12或
会有一些描述的错误。

然而,根据连接的顺序,它会成功并输出11
或22。




3.2 / 5的未定义行为。


单一定义规则允许您重复定义内联

的功能只要:

1)它们出现在不同的翻译单元中。

2)令牌代币相同,两个翻译单元中这些代币的含义

应该是相同的。


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

解决方案

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


* 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?



"Richard Hayden" <ra***@doc.ic.ac.uk> wrote in message
news:cb*********@sparta.btinternet.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.


这篇关于具有外部链接的内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆