C++ redefinition of log - static vars conflicting with included globals

118 Views Asked by At

I'm porting some code from Swift to C++. I was doing the following at the top of every Swift file:

private let log = os.Logger("Foo")

This defines a log variable that is local to the file. It's like static in C and C++.

If I try a similar thing in C++, in an implementation file (not header):

static MyLib::Logger log("Foo");

I get this error:

Redefinition of 'log' as different kind of symbol, previous definition is here:
/.../usr/include/c++/v1/math.h:977:1: log(_A1 __lcpp_x) _NOEXCEPT {return ::log((double)__lcpp_x);}

So it conflicts with the mathematical log function. Is there a way to keep my static log variable and somehow give it priority to resolve the name conflict error? Or do I just need to rename my log variable?

1

There are 1 best solutions below

1
Vektor 007 On

Well there are multiple ways to do it depending on your situation but the easiest way seems to be to just rename your log to something like _log or my_log. If not you can create your own namespace and put your log there.