Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 09:53 PM
Alex Vinokur
Guest
 
Posts: n/a
Default Method inlined in source1.cpp and called in source2.cpp

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




  #2  
Old July 22nd, 2005, 09:53 PM
Jason Heyes
Guest
 
Posts: n/a
Default Re: Method inlined in source1.cpp and called in source2.cpp

"Alex Vinokur" <alexvn@big-foot.com> wrote in message
news:2vr9anF2q4c1uU1@uni-berlin.de...[color=blue]
> 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[/color]

No. You have to inline the function in the header file.


  #3  
Old July 22nd, 2005, 09:53 PM
Gernot Frisch
Guest
 
Posts: n/a
Default Re: Method inlined in source1.cpp and called in source2.cpp


"Alex Vinokur" <alexvn@big-foot.com> schrieb im Newsbeitrag
news:2vr9anF2q4c1uU1@uni-berlin.de...[color=blue]
> 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
>
> -------------------[/color]

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


  #4  
Old July 22nd, 2005, 09:53 PM
angelo
Guest
 
Posts: n/a
Default Re: Method inlined in source1.cpp and called in source2.cpp

Gernot Frisch wrote:[color=blue]
> - 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[/color]

If I am correct, export is for templates only, right? So in his case
export is not applicable.
  #5  
Old July 22nd, 2005, 09:53 PM
Gernot Frisch
Guest
 
Posts: n/a
Default Re: Method inlined in source1.cpp and called in source2.cpp


"angelo" <minz0724@163.com> schrieb im Newsbeitrag
news:41987a8f$1@newsgate.hku.hk...[color=blue]
> Gernot Frisch wrote:[color=green]
>> - 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[/color]
>
> If I am correct, export is for templates only, right? So in his case
> export is not applicable.[/color]

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


  #6  
Old July 22nd, 2005, 09:53 PM
Thomas Maier-Komor
Guest
 
Posts: n/a
Default Re: Method inlined in source1.cpp and called in source2.cpp

Gernot Frisch wrote:[color=blue]
> "angelo" <minz0724@163.com> schrieb im Newsbeitrag
> news:41987a8f$1@newsgate.hku.hk...
>[color=green]
>>Gernot Frisch wrote:
>>[color=darkred]
>>>- 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[/color]
>>
>>If I am correct, export is for templates only, right? So in his case
>>export is not applicable.[/color]
>
>
> Ah. Sorry you're totally right. I was writing faster than thinking.
>[/color]

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
  #7  
Old July 22nd, 2005, 09:54 PM
Greg Comeau
Guest
 
Posts: n/a
Default Re: Method inlined in source1.cpp and called in source2.cpp

In article <2vr9q1F2nmgbvU1@uni-berlin.de>,
Gernot Frisch <Me@Privacy.net> wrote:[color=blue]
>
>"Alex Vinokur" <alexvn@big-foot.com> schrieb im Newsbeitrag
>news:2vr9anF2q4c1uU1@uni-berlin.de...[color=green]
>> 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
>>
>> -------------------[/color]
>
>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.[/color]

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?
  #8  
Old July 22nd, 2005, 09:54 PM
Greg Comeau
Guest
 
Posts: n/a
Default Re: Method inlined in source1.cpp and called in source2.cpp

In article <cn9vnv$7ba$1@wsc10.lrz-muenchen.de>,
Thomas Maier-Komor <maierkom@lpr.e-technik.no-spam.tu-muenchen.de> wrote:[color=blue]
>Gernot Frisch wrote:[color=green]
>> "angelo" <minz0724@163.com> schrieb im Newsbeitrag
>> news:41987a8f$1@newsgate.hku.hk...
>>[color=darkred]
>>>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.[/color]
>>
>>
>> Ah. Sorry you're totally right. I was writing faster than thinking.
>>[/color]
>
>I think you meant extern inline, which requires an inline declaration
>in all translation units and an extern inline definition only in one.[/color]

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).
[color=blue]
>This feature is supported by few compilers.[/color]

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?
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.