Multiple functions (one version being inline and other beingnon-inline)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rahul

    Multiple functions (one version being inline and other beingnon-inline)

    Hi Everyone,

    I have the following code,

    file1.cpp
    ---------

    #include <cstdio>

    inline int sample1()
    {
    printf("1::samp le1\n");
    return(0);
    }

    int main()
    {
    sample1();
    return(0);
    }


    file2.cpp
    ---------

    #include <cstdio>

    int sample1()
    {
    printf("2::samp le1\n");
    return(0);
    }


    when i build both the files and exeucte, i get the following output,

    2::sample1


    I expected a linker error, as file2.o is exporting sample1 which is
    already available in file1.o... What does the standard indicate for
    such scenarios?

    Thanks in advance !!!
  • =?ISO-8859-1?Q?Marcel_M=FCller?=

    #2
    Re: Multiple functions (one version being inline and other beingnon-inline)

    Rahul schrieb:
    I expected a linker error, as file2.o is exporting sample1 which is
    already available in file1.o... What does the standard indicate for
    such scenarios?
    You must not define the same object twice in a different way. Otherwise
    - as you might guess - undefined behaviour. (Look for the ODR.)


    Marcel

    Comment

    • nurxb01

      #3
      Re: Multiple functions (one version being inline and other beingnon-inline)

      On Feb 27, 11:28 pm, Marcel Müller <news.5.ma...@s pamgourmet.com>
      wrote:
      Rahul schrieb:
      >
      I expected a linker error, as file2.o is exporting sample1 which is
      already available in file1.o... What does the standard indicate for
      such scenarios?
      >
      I dont have knowledge of what standard says but I tried this with g++
      2.95.3 and g++ is doing is
      1] Keeps the sample1 funcion in file1.cpp as weak symbole in the
      object file ( Probably because you have made it inline )
      2] sample1 funcion in the file2.cpp is treated as Global symbol.

      So when you link this two object modules the Global symbole gets
      priority over Weak Symbol and you dont get any linker errors.

      If you remove inline in sample1 funcion form file1.cpp, you should get
      linker error.

      Comment

      • James Kanze

        #4
        Re: Multiple functions (one version being inline and other beingnon-inline)

        On Feb 27, 11:59 am, Rahul <sam_...@yahoo. co.inwrote:
        I have the following code,
        file1.cpp
        ---------
        #include <cstdio>
        >
        inline int sample1()
        {
        printf("1::samp le1\n");
        return(0);
        }
        int main()
        {
        sample1();
        return(0);
        }
        file2.cpp
        ---------
        #include <cstdio>
        int sample1()
        {
        printf("2::samp le1\n");
        return(0);
        }
        when i build both the files and exeucte, i get the following output,
        2::sample1
        I expected a linker error, as file2.o is exporting sample1
        which is already available in file1.o... What does the
        standard indicate for such scenarios?
        It's undefined behavior, so anything the compiler does with it
        is correct. §7.1.2/4: "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."

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        Working...