public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* g++: a bug or a feature ???
@ 2002-07-15 10:46 Yzhar Keysar
  2002-07-15 10:53 ` Gokhan Kisacikoglu
  0 siblings, 1 reply; 7+ messages in thread
From: Yzhar Keysar @ 2002-07-15 10:46 UTC (permalink / raw)
  To: gcc, gcc-help

Hi,

I have some linkage problems, probably relates to "6.4 Vague Linkage" 
section in the "Using GCC" document.

I'm working on a linux/Unix based project, where writing functors and 
using them  with the STL is a way of life.
when writing two fuctors ( or for simplicity : inline functions ) with 
the same prototype ( i.e. signature) but in different objects ,
I get no warning in compile time nor in linkage time. and runtime uses 
only ONE copy of them.

the problem is that in a large project, I could choose by mistake an 
already existed function prototype but have a different implementation.
thus using only one copy at runtime will be wrong. further more, using -O 
(optimization directive) inlines the corresponded function,
such that it calls to the correct function result in an inconsistency 
with non optimized objects.

I'm aware of the advantages of such feature ( as described for the weak 
(W) symbol ), but I really wish to have a warning of "duplicate 
declaration",
which I can turn off in case I wish it not.

I tried to turn of -fno-weak, but newly bigger problems have arisen from 
the depth. so I decided to stay with a quite well defined problem.



Example code:
-----------------------


//main.cc
#include <iostream>
#include "test.hh"

inline void g() { cerr<<"Main.g()\n" ; }

void main() {
    g();    // wish to activate g() in main.cc

    f();     // wish to activate g() in test.cc through f().
}
//--------end of main.cc

//test.cc
#include <iostream>
#include "test.hh"

inline void g() { cerr<<"Test.g()\n" ; }

void f() {
    g();    // wish to activate g() in test.cc
}
//--------end of test.cc

// test.hh

void f();
//--------end of test.hh


output:

Main.g()
Main.g()   *** WRONG ***


Working Environment
-------------------------------
compiler: g++_3.0.2 and g++_2.95.3
linker: GNU ld version 2.11.90.0.8 (with BFD 2.11.90.0.8)


thanx in advance
 -- 
Yzhar Keysar
Malcha Technological Park                     Tel: +972-2-6491476
Building No. 9                                Fax: +972-2-6491445
P.O.B 48182                                   new_user@silicon-value.com
Jerusalem 91481                               http://www.silicon-value.com


-- 
-- Yzhar Keysar

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: g++: a bug or a feature ???
  2002-07-15 10:46 g++: a bug or a feature ??? Yzhar Keysar
@ 2002-07-15 10:53 ` Gokhan Kisacikoglu
  2002-07-15 11:27   ` Oscar Fuentes
  0 siblings, 1 reply; 7+ messages in thread
From: Gokhan Kisacikoglu @ 2002-07-15 10:53 UTC (permalink / raw)
  To: Yzhar Keysar; +Cc: gcc, gcc-help

This is the correct code:


-----------------------
//main.cc
#include <iostream>
#include "test.hh"

inline void g() { cerr<<"Main.g()\n" ; }

void main() {
    g();    // wish to activate g() in main.cc

    f();     // wish to activate g() in test.cc through f().
}
//--------end of main.cc

//test.cc
#include <iostream>
#include "test.hh"

namespace test_cc
{
    inline void g() { cerr<<"Test.g()\n" ; }
}

void f() {
    test_cc :: g();    // wish to activate g() in test.cc
}

	---- OR ---------

using namespace test_cc;	// in the scope of test.cc

void f() {
    g();    // wish to activate g() in test.cc
}

//--------end of test.cc

// test.hh

void f();
//--------end of test.hh


The compiler has to make a decision on which g() to link with, obviously
the first one that is available is the one in main.

Gokhan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: g++: a bug or a feature ???
  2002-07-15 10:53 ` Gokhan Kisacikoglu
@ 2002-07-15 11:27   ` Oscar Fuentes
  0 siblings, 0 replies; 7+ messages in thread
From: Oscar Fuentes @ 2002-07-15 11:27 UTC (permalink / raw)
  To: kisa; +Cc: Yzhar Keysar, gcc-help

Gokhan Kisacikoglu <kisa@centropolisfx.com> writes:

> This is the correct code:
> 
> 
> -----------------------
> //main.cc
> #include <iostream>
> #include "test.hh"
> 
> inline void g() { cerr<<"Main.g()\n" ; }
> 
> void main() {
>     g();    // wish to activate g() in main.cc
> 
>     f();     // wish to activate g() in test.cc through f().
> }
> //--------end of main.cc
> 
> //test.cc
> #include <iostream>
> #include "test.hh"
> 
> namespace test_cc
> {
>     inline void g() { cerr<<"Test.g()\n" ; }
> }
> 
> void f() {
>     test_cc :: g();    // wish to activate g() in test.cc
> }
> 
> 	---- OR ---------
> 
> using namespace test_cc;	// in the scope of test.cc
> 
> void f() {
>     g();    // wish to activate g() in test.cc
> }

Why simply don't use an anonymous namespace? Why main.cc's 'g'
function is not wrapped in a namespace as well?

As a rule of thumb, put all your file-scope identifiers inside
anonymous namespaces. You will save some pains.

I guess that the problem described by the OP will not happen if it
declared the functions 'static' (which has a similar beahvior to
anonymous namespaces WRT this issue plus some dramatical
differences). And I guess too that the problem does not shows building
in release mode (that is, when 'inline' is honored).

 
> //--------end of test.cc
> 
> // test.hh
> 
> void f();
> //--------end of test.hh
> 
> 
> The compiler has to make a decision on which g() to link with, obviously
> the first one that is available is the one in main.

Here, the real problem is that you are mixing 'inline' (which
overrides the One Definition Rule) with multiple global definitions
*and* debug mode.

Other implementations would not allow this. It's 'ld' behavior what
allows it. (Not necessarily saying that 'ld' is broken...)

-- 
Oscar

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: g++: a bug or a feature ???
  2002-07-17  1:12 Yzhar Keysar
@ 2002-07-17  1:28 ` Nathan Sidwell
  0 siblings, 0 replies; 7+ messages in thread
From: Nathan Sidwell @ 2002-07-17  1:28 UTC (permalink / raw)
  To: Yzhar Keysar; +Cc: gcc, gcc-help

Yzhar Keysar wrote:
> when writing two fuctors ( or for simplicity : inline functions ) with
> the same prototype ( i.e. signature) but in different objects ,
> I get no warning in compile time nor in linkage time. and runtime uses
> only ONE copy of them.
As you realise, you are breaking the one definition rule [3.2].

> //main.cc
> #include <iostream>
> #include "test.hh"
> 
> inline void g() { cerr<<"Main.g()\n" ; }
you could make this static inline, if it only in one source file.
you could use namespaces (like the unnamed namespace)
namespace {
	inline void g ().....
}

void main () ...

of course it would be nice if the linker could check that two
merged symbols 'came from the same place' (but that's sometimes
not well defined).

nathan

-- 
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
           The voices in my head told me to say this
nathan@acm.org  http://www.cs.bris.ac.uk/~nathan/  nathan@cs.bris.ac.uk

^ permalink raw reply	[flat|nested] 7+ messages in thread

* g++: a bug or a feature ???
@ 2002-07-17  1:12 Yzhar Keysar
  2002-07-17  1:28 ` Nathan Sidwell
  0 siblings, 1 reply; 7+ messages in thread
From: Yzhar Keysar @ 2002-07-17  1:12 UTC (permalink / raw)
  To: gcc, gcc-help

Hi,

I have some linkage problems, probably relates to "6.4 Vague Linkage" 
section in the "Using GCC" document.

I'm working on a linux/Unix based project, where writing functors and 
using them  with the STL is a way of life.
when writing two fuctors ( or for simplicity : inline functions ) with 
the same prototype ( i.e. signature) but in different objects ,
I get no warning in compile time nor in linkage time. and runtime uses 
only ONE copy of them.

the problem is that in a large project, I could choose by mistake an 
already existed function prototype but have a different implementation.
thus using only one copy at runtime will be wrong. further more, using -O 
(optimization directive) inlines the corresponded function,
such that it calls to the correct function result in an inconsistency 
with non optimized objects.

I'm aware of the advantages of such feature ( as described for the weak 
(W) symbol ), but I really wish to have a warning of "duplicate 
declaration",
which I can turn off in case I wish it not.

I tried to turn of -fno-weak, but newly bigger problems have arisen from 
the depth. so I decided to stay with a quite well defined problem.



Example code:
-----------------------


//main.cc
#include <iostream>
#include "test.hh"

inline void g() { cerr<<"Main.g()\n" ; }

void main() {
    g();    // wish to activate g() in main.cc

    f();     // wish to activate g() in test.cc through f().
}
//--------end of main.cc

//test.cc
#include <iostream>
#include "test.hh"

inline void g() { cerr<<"Test.g()\n" ; }

void f() {
    g();    // wish to activate g() in test.cc
}
//--------end of test.cc

// test.hh

void f();
//--------end of test.hh


output:

Main.g()
Main.g()   *** WRONG ***


Working Environment
-------------------------------
compiler: g++_3.0.2 and g++_2.95.3
linker: GNU ld version 2.11.90.0.8 (with BFD 2.11.90.0.8)


thanx in advance
 -- Yzhar Keysar

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: g++: a bug or a feature ???
  2002-07-09  7:33 Yzhar Keysar
@ 2002-07-09 10:13 ` Gokhan Kisacikoglu
  0 siblings, 0 replies; 7+ messages in thread
From: Gokhan Kisacikoglu @ 2002-07-09 10:13 UTC (permalink / raw)
  To: Yzhar Keysar; +Cc: gcc-help

you should use namespaces around your functions, especially with the
same names, you can easily choose whatever you want then.

Yzhar Keysar wrote:
> 
> Hi,
> 
> I have some linkage problems, probably relates to "6.4 Vague Linkage"
> section in the "Using GCC" document.
> 
> I'm working on a linux/Unix based project, where writing functors and
> using them  with the STL is a way of life.
> when writing two fuctors ( or for simplicity : inline functions ) with
> the same prototype ( i.e. signature) but in different objects ,
> I get no warning in compile time nor in linkage time. and runtime uses
> only ONE copy of them.
> 
> the problem is that in a large project, I could choose by mistake an
> already existed function prototype but have a different implementation.
> thus using only one copy at runtime will be wrong. further more, using -O
> (optimization directive) inlines the corresponded function,
> such that it calls to the correct function result in an inconsistency
> with non optimized objects.
> 
> I'm aware of the advantages of such feature ( as described for the weak
> (W) symbol ), but I really wish to have a warning of "duplicate
> declaration",
> which I can turn off in case I wish it not.
> 
> I tried to turn of -fno-weak, but newly bigger problems have arisen from
> the depth. so I decided to stay with a quite well defined problem.
> 
> Example code:
> -----------------------
> 
> //main.cc
> #include <iostream>
> #include "test.hh"
> 
> inline void g() { cerr<<"Main.g()\n" ; }
> 
> void main() {
>     g();    // wish to activate g() in main.cc
> 
>     f();     // wish to activate g() in test.cc through f().
> }
> //--------end of main.cc
> 
> //test.cc
> #include <iostream>
> #include "test.hh"
> 
> inline void g() { cerr<<"Test.g()\n" ; }
> 
> void f() {
>     g();    // wish to activate g() in test.cc
> }
> //--------end of test.cc
> 
> // test.hh
> 
> void f();
> //--------end of test.hh
> 
> output:
> 
> Main.g()
> Main.g()   *** WRONG ***
> 
> Working Environment
> -------------------------------
> compiler: g++_3.0.2 and g++_2.95.3
> linker: GNU ld version 2.11.90.0.8 (with BFD 2.11.90.0.8)
> 
> thanx in advance
>  --
> Yzhar Keysar
> Malcha Technological Park                     Tel: +972-2-6491476
> Building No. 9                                Fax: +972-2-6491445
> P.O.B 48182                                   new_user@silicon-value.com
> Jerusalem 91481                               http://www.silicon-value.com

-- 
Gökhan Kisacikoglu    			     Centropolis Effects, LLC
Senior Technical Director		     10950 W Washington Blvd
kisa@centropolisfx.com   310.204.7300 x263   Culver City, CA, 90232
Key: Gö(GIrl)-khan Kis(cirCUS)-a-(Art)-cik(loGIC)-og(thOUGH)-lu(fLU)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* g++: a bug or a feature ???
@ 2002-07-09  7:33 Yzhar Keysar
  2002-07-09 10:13 ` Gokhan Kisacikoglu
  0 siblings, 1 reply; 7+ messages in thread
From: Yzhar Keysar @ 2002-07-09  7:33 UTC (permalink / raw)
  To: gcc-help


Hi,

I have some linkage problems, probably relates to "6.4 Vague Linkage" 
section in the "Using GCC" document.

I'm working on a linux/Unix based project, where writing functors and 
using them  with the STL is a way of life.
when writing two fuctors ( or for simplicity : inline functions ) with 
the same prototype ( i.e. signature) but in different objects ,
I get no warning in compile time nor in linkage time. and runtime uses 
only ONE copy of them.

the problem is that in a large project, I could choose by mistake an 
already existed function prototype but have a different implementation.
thus using only one copy at runtime will be wrong. further more, using -O 
(optimization directive) inlines the corresponded function,
such that it calls to the correct function result in an inconsistency 
with non optimized objects.

I'm aware of the advantages of such feature ( as described for the weak 
(W) symbol ), but I really wish to have a warning of "duplicate 
declaration",
which I can turn off in case I wish it not.

I tried to turn of -fno-weak, but newly bigger problems have arisen from 
the depth. so I decided to stay with a quite well defined problem.



Example code:
-----------------------

//main.cc
#include <iostream>
#include "test.hh"

inline void g() { cerr<<"Main.g()\n" ; }

void main() {
    g();    // wish to activate g() in main.cc

    f();     // wish to activate g() in test.cc through f().
}
//--------end of main.cc

//test.cc
#include <iostream>
#include "test.hh"

inline void g() { cerr<<"Test.g()\n" ; }

void f() {
    g();    // wish to activate g() in test.cc
}
//--------end of test.cc

// test.hh

void f();
//--------end of test.hh


output:

Main.g()
Main.g()   *** WRONG ***




Working Environment
-------------------------------
compiler: g++_3.0.2 and g++_2.95.3
linker: GNU ld version 2.11.90.0.8 (with BFD 2.11.90.0.8)


thanx in advance
 -- 
Yzhar Keysar
Malcha Technological Park                     Tel: +972-2-6491476
Building No. 9                                Fax: +972-2-6491445
P.O.B 48182                                   new_user@silicon-value.com
Jerusalem 91481                               http://www.silicon-value.com


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2002-07-17  8:28 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-15 10:46 g++: a bug or a feature ??? Yzhar Keysar
2002-07-15 10:53 ` Gokhan Kisacikoglu
2002-07-15 11:27   ` Oscar Fuentes
  -- strict thread matches above, loose matches on Subject: below --
2002-07-17  1:12 Yzhar Keysar
2002-07-17  1:28 ` Nathan Sidwell
2002-07-09  7:33 Yzhar Keysar
2002-07-09 10:13 ` Gokhan Kisacikoglu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).