Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Multiple functions (one version being inline and other beingnon-inline)

Reply
Thread Tools

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

 
 
Rahul
Guest
Posts: n/a
 
      02-27-2008
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 !!!
 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      02-27-2008
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
 
Reply With Quote
 
 
 
 
nurxb01
Guest
Posts: n/a
 
      02-28-2008
On Feb 27, 11:28*pm, Marcel Müller <(E-Mail Removed)>
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.

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-28-2008
On Feb 27, 11:59 am, Rahul <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Where to get stand alone Dot Net Framework version 1.1, version2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? MowGreen [MVP] ASP .Net 5 02-09-2008 01:55 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? PA Bear [MS MVP] ASP .Net 0 02-05-2008 03:28 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? V Green ASP .Net 0 02-05-2008 02:45 AM
Tool which expands implicitly inline inline functions tthunder@gmx.de C++ 3 06-16-2005 12:54 AM
External inline functions calling internal inline functions Daniel Vallstrom C Programming 2 11-21-2003 01:57 PM



Advertisments