Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Symbol already defined acting differently for class methods?

42 views
Skip to first unread message

blazb

unread,
Oct 22, 2013, 9:20:58 AM10/22/13
to
Why does the linker (gcc, msvc) not fail for the example below?

It complains for multiple definitions of function bar, but not for multiple definitions of function foo in class Foo.


"a.cpp"

#include <stdio.h>

void bar() {
puts("bar from a.cpp");
}

struct Foo {
char a[20];
void foo(){
puts("foo from a.cpp");
}
};

void test_b();

int main()
{
printf("sizeof(Foo) in a.cpp: %d\n", sizeof(Foo));
Foo().foo();
bar();
test_b();
return 0;
}

"b.cpp"

#include <stdio.h>

struct Foo {
char a[20];
void foo(){
puts("foo from b.cpp");
}
};

//void bar() { puts("bar from b.cpp"); } // error: symbol already defined

void test_b()
{
printf("sizeof(Foo) in b.cpp: %d\n", sizeof(Foo));
Foo().foo();
// bar();
}

sg

unread,
Oct 22, 2013, 9:48:33 AM10/22/13
to
Am 22.10.2013 15:20, schrieb blazb:
> Why does the linker (gcc, msvc) not fail for the example below?
>
> It complains for multiple definitions of function bar, but not for multiple definitions of function foo in class Foo.

That's because foo is defined inside a class and therefore implicitly
inline. The one definition rule makes an exception for inline functions
(as well as function templates and static data members of class templates).

blaz.b...@gmail.com

unread,
Oct 22, 2013, 10:02:16 AM10/22/13
to
Thanks for yout prompt reponse.

However, its not clear to me, why the same member function is called from call in a and call in b.

with
g++ a.cpp b.cpp
foo from a.cpp is called in all cases.

and with
g++ b.cpp a.cpp
foo from b.cpp is called in all cases.

thanks

blaz.b...@gmail.com

unread,
Oct 22, 2013, 10:06:56 AM10/22/13
to
ah, nvm. This was the case only for compilation with -O0, but works ok with -O1,2 and 3.

thanks

Tobias Müller

unread,
Oct 22, 2013, 11:58:34 AM10/22/13
to
<blaz.b...@gmail.com> wrote:
[cleaned up quoting mess]
Careful!
Classes an inline functions can be defined in multiple translation units,
but all definitions must be the same. Everything else is undefined behavior
and _anything_ can happen.

Tobi

Richard Damon

unread,
Oct 23, 2013, 11:27:48 PM10/23/13
to
On 10/22/13 9:20 AM, blazb wrote:
> Why does the linker (gcc, msvc) not fail for the example below?
>
> It complains for multiple definitions of function bar, but not for multiple definitions of function foo in class Foo.
>
>

As has been mentioned, Foo::foo is declared (implicitly) inline. Now
inline functions may need to generate a "real" version of the function
to call when it can't be made inline for some reason, and the compiler
is obligated to make this work. Typically, the compiler will mark the
object code generated in some way so the linker will throw out the extra
copies automatically.

blaz.b...@gmail.com

unread,
Oct 24, 2013, 8:55:38 AM10/24/13
to
Thanks for all replies.
0 new messages