multiple definition of inline function

Posted by K71993 on Stack Overflow See other posts from Stack Overflow or by K71993
Published on 2010-04-27T14:58:11Z Indexed on 2010/04/27 15:03 UTC
Read the original article Hit count: 342

Filed under:
|

Hi,

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

Code Description

I have a header file "inline.h" and two translation unit "main.cpp" and "tran.cpp". Details of code are as below

  1. inline.h file details
    #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

  2. main.c file details are below
    #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;
    }

  3. tran.cpp file details (Not 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; }

Question

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

Can you clarify why this difference is present across compile?

  1. When you run the program (g++ compiled) by creating two seperate compilation unit (main.o and tran.o and create an executable a.out), the output obtained is
    500
    6
    700

Why does the compiler pick up the definition of the function which is not inline. Actually since #include is used to "add" the inline definiton 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.
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.

Thanks in advance for valuable input.

© Stack Overflow or respective owner

Related posts about inline

Related posts about c