DeMarcus <> writes:
> DeMarcus wrote:
>> Hi,
>>
>> Will two symbols with the same name but in different .cpp files clash?
>> E.g.
>>
>> file1.cpp
>>
>> int varA;
>>
>>
>> file2.cpp
>>
>> int varA;
>>
>>
>> Will varA clash during linking? Of course I can test it myself, but
>> is there a possibility that they are allowed to clash but the
>> compiler chooses one of them?
>>
>> Sometimes I've seen compiler messages like: "multiple definitions of
>> X, ignoring the latter".
>>
>>
>> Thanks,
>> Daniel
>
> Wait, I just came to think of that I have to declare them static!
> I.e.
>
> file1.cpp
>
> static int varA;
>
> file2.cpp
>
> static int varA;
>
>
> It was a long time since I used that.
This particular use of the static keyword is deprecated in C++, IIRC.
To ensure that your two variables are not visible outside the
translation unit they are required in, use un-named namespaces:
// file1.cpp
namespace {
int varA;
}
// file2.cpp
namespace {
int varA;
}
Regards
Paul Bibbings