内联函数的多个定义 [英] multiple definition of inline function

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

问题描述

我已经经历与此主题有关一些职位走了,但并没有能够完全理清我的怀疑。这可能是一个非常幼稚的问题。

I have gone through some posts related to this topic but was not able to sort out my doubt completely. This might be a very naive question.

我有一个头文件 inline.h 和两个翻译单元的main.cpp tran.cpp

I have a header file inline.h and two translation units main.cpp and tran.cpp.

code的详情如下

#ifndef __HEADER__
#include <stdio.h>
extern inline int func1(void)
{ return 5; }

static inline int func2(void)
{ return 6; }

inline int func3(void)
{ return 7; }
#endif

的main.c

#define <stdio.h>
#include <inline.h>
int main(int argc, char *argv[])
{
    printf("%d\n",func1());
    printf("%d\n",func2());
    printf("%d\n",func3());
    return 0;
}

tran.cpp

//(note that the functions are not inline here)
#include <stdio.h>
int func1(void)
{ return 500; }

int func2(void)
{ return 600; }

int func3(void)
{ return 700; }

以上code编译在G ++,但是在GCC不编译(即使你做出的gcc像改变code到.c来,不使用任何C ++头文件等相关的更改)。显示的错误是内联函数的重复定义 - FUNC3。

The above code compiles in g++, but does not compile in gcc (even if you make changes related to gcc like changing the code to .c, not using any C++ header files, etc.). The error displayed is "duplicate definition of inline function - func3".

可以解释,为什么这种差异是跨编译器present?

Can you clarify why this difference is present across compilers?

此外,当您通过创建两个单独的编译单元运行的程序(编译G ++)( main.o中 tran.o ),并创建一个可执行的a.out ,得到的输出是:

Also, when you run the program (g++ compiled) by creating two separate compilation units (main.o and tran.o) and create an executable a.out, the output obtained is:

500结果
  6结果
  700

500
6
700

为什么编译器拾取这不是内联函数的定义。实际上,因为的#include 是用来补充我预料的内联定义 5,6,7 作为输出。我的理解是在编译过程中,因为行内定义被发现,函数调用将被内联函数定义改为

Why does the compiler pick up the definition of the function which is not inline. Actually, since #include is used to "add" the inline definition I had expected 5,6,7 as the output. My understanding was during compilation since the inline definition is found, the function call would be "replaced" by inline function definition.

你能告诉我详细的步骤编译和链接的过程中,这将导致我们 500,6,700 输出。我只能理解输出6。

Can you please tell me in detailed steps the process of compilation and linking which would lead us to 500,6,700 output. I can only understand the output 6.

推荐答案

这答案分为以下几个部分:

This answer is divided into the following sections:


  1. 如何重现inline函数的重复定义 - FUNC3 的问题,为什么

  2. 为什么 FUNC3 的确定指标是重复的,而不是 FUNC1

  3. 为什么它编译使用 G ++

  1. How to reproduce the duplicate definition of inline function - func3 problem and why.
  2. Why defintion of func3 is a duplicate instead of func1.
  3. Why it compiles using g++

问题可以通过


  1. 重命名 tran.cpp tran.c

  2. 的gcc -o主要的main.c tran.c编译

  1. Rename tran.cpp to tran.c
  2. Compile with gcc -o main main.c tran.c

@ K71993使用旧gnu89内联的语义,这是C99不同的实际编制。之所以改名 tran.cpp tran.c 是告诉GCC驱动器将其视为 C 来源,而不是 C ++ 来源。

@K71993 is actually compiling using the old gnu89 inline semantics, which is different from C99. The reason for renaming tran.cpp to tran.c is to tell the gcc driver to treat it as C source instead of C++ source.

下面的文字是从 GCC文件引用:inline函数一样快作为宏解释了为什么 FUNC3 是重复定义,而不是 FUNC1 ,因为 FUNC3 (而不是 FUNC1 )是一个外部可见的符号(在GNU89内嵌语义)

The following text is quoted from GCC Document: An Inline Function is As Fast As a Macro explains why func3 is a duplicate definition instead of func1, since func3 (instead of func1) is an externally visible symbol (in GNU89 inline semantics)

当inline函数不是静态的,那么编译器必须假定有可能是其他源文件调用;因为全局符号只能在一次所有程序中定义,功能不能在其他源文件中定义,所以调用不能被集成。因此,非static的内联函数总是编译自身在通常的方式。

When an inline function is not static, then the compiler must assume that there may be calls from other source files; since a global symbol can be defined only once in any program, the function must not be defined in the other source files, so the calls therein cannot be integrated. Therefore, a non-static inline function is always compiled on its own in the usual fashion.

如果您指定的内联和extern在函数定义,然后定义仅用于内联。 在任何情况下对自己的编译功能,没有就算你是指它的地址明确。这种地址变成了一个外部引用,就好像你只是声明了函数,却没有定义它。

If you specify both inline and extern in the function definition, then the definition is used only for inlining. In no case is the function compiled on its own, not even if you refer to its address explicitly. Such an address becomes an external reference, as if you had only declared the function, and had not defined it.

如果用C99标准,即 GCC编译-o主要的main.c tran.c -std = C99 ,链接器会抱怨这个定义 FUNC1 是重复的原因在于 extern内联在C99是一个外部定义在其他文章和评论中提到的原因。

C99 inline semantics

If compiled with C99 standard, i.e., gcc -o main main.c tran.c -std=c99, the linker will complain that definition of func1 is a duplicate due to the reason that extern inline in C99 is a external definition as mentioned in other posts and comments.

请同时参阅此了解 GNU89内联 C99直列。

在与 G ++ 编译源程序被视为 C ++ 来源。由于 FUNC1 FUNC2 FUNC3 在多个翻译定义单位和他们的定义是不同的,C的一个规则确定指标++ 是侵犯。由于编译器不需要生成时定义跨越多个翻译单元dignostic消息,该行为是不确定的。

When compiled with g++, the source program are considered as C++ source. Since func1, func2 and func3 are defined in multiple translation units and their definitions are different, the One Defintion Rule of C++ is violated. Since the compiler is not required to generate dignostic message when definitions spans multiple translation units, the behavior is undefined.

这篇关于内联函数的多个定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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