Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Duplicate symbo...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 2 Topic 9578 of 9592
Post > Topic >>

Duplicate symbols combined without warning

by nils.hjelte@[EMAIL PROTECTED] May 7, 2008 at 11:43 AM

Hi!

Not really sure where to post this, maybe the gcc list instead?

Anyway, if I have two cpp files with identical class declarations (but
with different semantics) of an internal class, and link them
together, one of the implementations get thrown out without any kind
of warning.

Simple example:

foo.cpp
=========================================

#include <iostream>

class DuplicateClass
{
public:
   void foo() { std::cout << "foo" << std::endl; }
   void foobar() { std::cout << "first foobar" << std::endl; }
};

void foo()
{
   DuplicateClass test;
   test.foo();
   test.foobar();
}


bar.cpp
=========================================
#include <iostream>

class DuplicateClass
{
public:
   void bar() { std::cout << "bar" << std::endl; }
   void foobar() { std::cout << "second foobar" << std::endl; }
};

void bar()
{
   DuplicateClass test;
   test.bar();
   test.foobar();
}

main.cpp
==========================================
void foo();
void bar();

int main (int argc, char const *argv[])
{
   foo();
   bar();
   return 0;
}



Compiled with

g++ -o test main.cpp foo.cpp bar.cpp -Wall

I get the following output when executing the program

foo
first foobar
bar
first foobar

The test.foobar() call in bar.cpp should output 'second foobar' not
'first foobar'. Looking at the symbols in the executable file it is
obvious that one of the colliding symbols have been thrown out (only
one foobar entry):

$ nm test | grep DuplicateClass
00001c96 T __ZN14DuplicateClass3barEv
00001bce T __ZN14DuplicateClass3fooEv
00001c12 T __ZN14DuplicateClass6foobarEv

Why won't the compiler/linker give me a warning/error when doing this?
I had a really nasty bug resulting from this, which was pretty
difficult to track down. I tried declaring the classes with 'static
class' thinking it would work as 'static function' does in C, but it
is obviously not valid C++ syntax.

Is it not possible to limit the scope of the class to the local cpp
file? And why does not the compiler provide a warning when this
happens??

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]




 2 Posts in Topic:
Duplicate symbols combined without warning
nils.hjelte@[EMAIL PROTEC  2008-05-07 11:43:28 
Re: Duplicate symbols combined without warning
=?ISO-8859-1?Q?Daniel_Kr=  2008-05-07 18:33:02 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon May 12 18:12:34 CDT 2008.