This happens when one class is defined in header file, the other defined in cpp file, have the same name. The program behavior becomes gcc argument order dependent. At any rate, class A defined in foo.cpp has file scope shouldn't be seen in bar.cpp.
$ g++ main.cpp foo.cpp bar.cpp $ ./a.out foo.cpp: A::A() foo.cpp: A::~A() foo.cpp: A::A() foo.cpp: A::~A() $ g++ main.cpp bar.cpp foo.cpp $ ./a.out bar.h: A::A() bar.h: A::~A() bar.h: A::A() bar.h: A::~A() -- main.cpp -- #include <iostream> #include "foo.h" #include "bar.h" int main() { foo f; bar b; return 1; } -- foo.h -- class foo { public: foo(); }; -- foo.cpp -- #include "foo.h" #include <iostream> class A { public: A() { std::cout << "foo.cpp: A::A()" << std::endl; } ~A() { std::cout << "foo.cpp: A::~A()" << std::endl; } }; foo::foo() { A * a = new A(); delete a; } -- bar.h -- #include <iostream> class A { public: A() { std::cout << "bar.h: A::A()" << std::endl; } ~A() { std::cout << "bar.h: A::~A()" << std::endl; } }; class bar { public: bar(); }; -- bar.cpp -- #include "bar.h" #include <iostream> bar::bar() { A *a = new A(); delete a; } -- Summary: class defined in file scope got linked by other files Product: gcc Version: 4.2.4 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: jengliang at gmail dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39513