I'm trying to debug a seg fault that's occurring in my team's codebase. There's a hash_key
that is defined in two files file1.cpp, file2.cpp
. It is not wrapped with an anonymous namespace, so it seems to just be a global definition within the file, e.g.,
In file1.cpp
, we define
struct hash_key {
int a;
int b;
};
// programs below use hash_key
In file2.cpp
, we define
struct hash_key {
string s;
};
// programs below use hash_key
(Note the hash_key
defined in each file have no overlapping variable names)
I don't think anything in file1.cpp
should see the contents of file2.cpp
and vice versa. However, when I wrap an anonymous namespace around the hash_key
in file2.cpp
, the seg fault goes away, so now I'm questioning whether my understanding of multi-file C++ compilation is correct.
When the contents of file1
uses hash_key
, it should be using the hash_key
struct defined within that file right?
It could be something else that's causing the seg fault, but right now, I'm empirically observing that having the namespace in file2.cpp
removes the seg fault issue.