Method inlined in source1.cpp and called in source2.cpp

Discussion in 'C++' started by Alex Vinokur, Nov 15, 2004.

  1. Alex Vinokur

    Alex Vinokur Guest

    Is it possible to call in source2.cpp a method inlined in source1.cpp?

    --- File foo.h ---
    #ifndef FOO_H
    #define FOO_H

    struct Foo { void foo (); };

    #endif
    ------------------


    --- File foo1.cpp ---
    #include "foo.h"
    inline void Foo::foo() {}
    ---------------------


    --- File foo2.cpp ---
    #include "foo.h"

    int main ()
    {
    Foo f;
    f.foo();

    return 0;
    }
    ---------------------


    --- Compilation ---

    $ gpp --version
    gpp.exe (GCC) 3.4.1
    [---omitted---]

    $ gpp foo1.cpp foo2.cpp
    c:/djgpp/tmp/cckdogiI.o(.text+0x24):foo2.cpp: undefined reference to `Foo::foo()'
    collect2: ld returned 1 exit status

    -------------------


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Nov 15, 2004
    #1
    1. Advertising

  2. Alex Vinokur

    Jason Heyes Guest

    "Alex Vinokur" <> wrote in message
    news:...
    > Is it possible to call in source2.cpp a method inlined in source1.cpp?
    >
    > --- File foo.h ---
    > #ifndef FOO_H
    > #define FOO_H
    >
    > struct Foo { void foo (); };
    >
    > #endif
    > ------------------
    >
    >
    > --- File foo1.cpp ---
    > #include "foo.h"
    > inline void Foo::foo() {}
    > ---------------------
    >
    >
    > --- File foo2.cpp ---
    > #include "foo.h"
    >
    > int main ()
    > {
    > Foo f;
    > f.foo();
    >
    > return 0;
    > }
    > ---------------------
    >
    >
    > --- Compilation ---
    >
    > $ gpp --version
    > gpp.exe (GCC) 3.4.1
    > [---omitted---]
    >
    > $ gpp foo1.cpp foo2.cpp
    > c:/djgpp/tmp/cckdogiI.o(.text+0x24):foo2.cpp: undefined reference to
    > `Foo::foo()'
    > collect2: ld returned 1 exit status
    >
    > -------------------
    >
    >
    > --
    > Alex Vinokur
    > email: alex DOT vinokur AT gmail DOT com
    > http://mathforum.org/library/view/10978.html
    > http://sourceforge.net/users/alexvn


    No. You have to inline the function in the header file.
    Jason Heyes, Nov 15, 2004
    #2
    1. Advertising

  3. "Alex Vinokur" <> schrieb im Newsbeitrag
    news:...
    > Is it possible to call in source2.cpp a method inlined in
    > source1.cpp?
    >
    > --- File foo.h ---
    > #ifndef FOO_H
    > #define FOO_H
    >
    > struct Foo { void foo (); };
    >
    > #endif
    > ------------------
    >
    >
    > --- File foo1.cpp ---
    > #include "foo.h"
    > inline void Foo::foo() {}
    > ---------------------
    >
    >
    > --- File foo2.cpp ---
    > #include "foo.h"
    >
    > int main ()
    > {
    > Foo f;
    > f.foo();
    >
    > return 0;
    > }
    > ---------------------
    >
    >
    > --- Compilation ---
    >
    > $ gpp --version
    > gpp.exe (GCC) 3.4.1
    > [---omitted---]
    >
    > $ gpp foo1.cpp foo2.cpp
    > c:/djgpp/tmp/cckdogiI.o(.text+0x24):foo2.cpp: undefined reference to
    > `Foo::foo()'
    > collect2: ld returned 1 exit status
    >
    > -------------------


    3 ways:
    - inline in the header
    - write an .inc file and include it in the header (for the compiler
    that same as writing in the header)
    - get an compiler that can handle the export keyword - there's ony one
    from comeau and for portability I suggest not to use it.

    HTH,
    Gernot
    Gernot Frisch, Nov 15, 2004
    #3
  4. Alex Vinokur

    angelo Guest

    Gernot Frisch wrote:
    > - get an compiler that can handle the export keyword - there's ony one
    > from comeau and for portability I suggest not to use it.
    >
    > HTH,
    > Gernot


    If I am correct, export is for templates only, right? So in his case
    export is not applicable.
    angelo, Nov 15, 2004
    #4
  5. "angelo" <> schrieb im Newsbeitrag
    news:41987a8f$...
    > Gernot Frisch wrote:
    >> - get an compiler that can handle the export keyword - there's ony
    >> one from comeau and for portability I suggest not to use it.
    >>
    >> HTH,
    >> Gernot

    >
    > If I am correct, export is for templates only, right? So in his case
    > export is not applicable.


    Ah. Sorry you're totally right. I was writing faster than thinking.
    Gernot Frisch, Nov 15, 2004
    #5
  6. Gernot Frisch wrote:
    > "angelo" <> schrieb im Newsbeitrag
    > news:41987a8f$...
    >
    >>Gernot Frisch wrote:
    >>
    >>>- get an compiler that can handle the export keyword - there's ony
    >>>one from comeau and for portability I suggest not to use it.
    >>>
    >>>HTH,
    >>>Gernot

    >>
    >>If I am correct, export is for templates only, right? So in his case
    >>export is not applicable.

    >
    >
    > Ah. Sorry you're totally right. I was writing faster than thinking.
    >


    I think you meant extern inline, which requires an inline declaration
    in all translation units and an extern inline definition only in one.
    This feature is supported by few compilers.

    Tom
    Thomas Maier-Komor, Nov 15, 2004
    #6
  7. Alex Vinokur

    Greg Comeau Guest

    In article <>,
    Gernot Frisch <> wrote:
    >
    >"Alex Vinokur" <> schrieb im Newsbeitrag
    >news:...
    >> Is it possible to call in source2.cpp a method inlined in
    >> source1.cpp?
    >>
    >> --- File foo.h ---
    >> #ifndef FOO_H
    >> #define FOO_H
    >>
    >> struct Foo { void foo (); };
    >>
    >> #endif
    >> ------------------
    >>
    >>
    >> --- File foo1.cpp ---
    >> #include "foo.h"
    >> inline void Foo::foo() {}
    >> ---------------------
    >>
    >>
    >> --- File foo2.cpp ---
    >> #include "foo.h"
    >>
    >> int main ()
    >> {
    >> Foo f;
    >> f.foo();
    >>
    >> return 0;
    >> }
    >> ---------------------
    >>
    >>
    >> --- Compilation ---
    >>
    >> $ gpp --version
    >> gpp.exe (GCC) 3.4.1
    >> [---omitted---]
    >>
    >> $ gpp foo1.cpp foo2.cpp
    >> c:/djgpp/tmp/cckdogiI.o(.text+0x24):foo2.cpp: undefined reference to
    >> `Foo::foo()'
    >> collect2: ld returned 1 exit status
    >>
    >> -------------------

    >
    >3 ways:
    >- inline in the header
    >- write an .inc file and include it in the header (for the compiler
    >that same as writing in the header)
    >- get an compiler that can handle the export keyword - there's ony one
    >from comeau and for portability I suggest not to use it.


    If you mean portability across OSs, well Comeau supports the popular
    ones and we are always willing and capable to bring it to others,
    and in a reasonable timeframe and cost, and of course already do custom
    ports with a goal of complete functionality and parts. If you mean
    portability across compilers, then sure, this should be given
    consideration.

    BTW, I may be missing the point, but it seems to me that the
    OPS question is strictly about cross translation unit inline'ing,
    which hence makes suggesting export mostly a neutral issue.
    --
    Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Nov 15, 2004
    #7
  8. Alex Vinokur

    Greg Comeau Guest

    In article <cn9vnv$7ba$-muenchen.de>,
    Thomas Maier-Komor <-technik.no-spam.tu-muenchen.de> wrote:
    >Gernot Frisch wrote:
    >> "angelo" <> schrieb im Newsbeitrag
    >> news:41987a8f$...
    >>
    >>>Gernot Frisch wrote:
    >>>
    >>>>- get an compiler that can handle the export keyword - there's ony
    >>>>one from comeau and for portability I suggest not to use it.
    >>>>
    >>>>HTH,
    >>>>Gernot
    >>>
    >>>If I am correct, export is for templates only, right? So in his case
    >>>export is not applicable.

    >>
    >>
    >> Ah. Sorry you're totally right. I was writing faster than thinking.
    >>

    >
    >I think you meant extern inline, which requires an inline declaration
    >in all translation units and an extern inline definition only in one.


    I suspect that you guys are still trying to compare this to export,
    that is, every translation unit which uses an inline function should
    define it as per ODRing (of course a given compiler may be smarter
    than this, but that's a seperate issue).

    >This feature is supported by few compilers.


    Comeau C++ can support extern inline functions too.
    --
    Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Nov 15, 2004
    #8
    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. Andy
    Replies:
    3
    Views:
    287
  2. Steven T. Hatton

    What gets inlined?

    Steven T. Hatton, May 30, 2005, in forum: C++
    Replies:
    15
    Views:
    612
    Steven T. Hatton
    Jun 1, 2005
  3. Ognen Duzlevski

    Inlined C code and a TypeError

    Ognen Duzlevski, Jan 13, 2004, in forum: Python
    Replies:
    0
    Views:
    283
    Ognen Duzlevski
    Jan 13, 2004
  4. inline functions not inlined

    , Mar 6, 2006, in forum: C Programming
    Replies:
    9
    Views:
    314
    David Resnick
    Mar 7, 2006
  5. sks
    Replies:
    4
    Views:
    302
    Victor Bazarov
    Dec 6, 2005
Loading...

Share This Page