B
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.
"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();
}
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();
}