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

Discussion in 'C++' started by Rahul, Feb 27, 2008.

  1. Rahul

    Rahul Guest

    Hi Everyone,

    I have the following code,

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

    #include <cstdio>

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

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


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

    #include <cstdio>

    int sample1()
    {
    printf("2::sample1\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 !!!
     
    Rahul, Feb 27, 2008
    #1
    1. Advertising

  2. 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
     
    Marcel Müller, Feb 27, 2008
    #2
    1. Advertising

  3. Rahul

    nurxb01 Guest

    On Feb 27, 11:28 pm, Marcel Müller <>
    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.
     
    nurxb01, Feb 28, 2008
    #3
  4. Rahul

    James Kanze Guest

    On Feb 27, 11:59 am, Rahul <> wrote:
    > I have the following code,


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


    > #include <cstdio>
    >
    > inline int sample1()
    > {
    > printf("1::sample1\n");
    > return(0);
    > }


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


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


    > #include <cstdio>


    > int sample1()
    > {
    > printf("2::sample1\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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 28, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. loveNUNO
    Replies:
    2
    Views:
    955
    loveNUNO
    Nov 20, 2003
  2. Replies:
    3
    Views:
    483
  3. Daniel Vallstrom
    Replies:
    2
    Views:
    2,005
    Kevin Bracey
    Nov 21, 2003
  4. V Green
    Replies:
    0
    Views:
    903
    V Green
    Feb 5, 2008
  5. PA Bear [MS MVP]
    Replies:
    0
    Views:
    1,018
    PA Bear [MS MVP]
    Feb 5, 2008
Loading...

Share This Page