I’m trying to do this:
#pragma once
#include <fstream>
#include <string>
static std::ofstream ErrorLog;
void InitErrorLog(std::string FileName) {
ErrorLog.open(FileName);
}
but am getting a “One or more multiply defined symbols found” error when #include-ing in multiple CPP files. What is the STL doing (to provide cout, cin, cerr, etc. — this approach originates as an alternative to redirecting cerr) that I’m not?
You’re breaking the one definition rule. You need to make the method
inline.Also, note that by declaring your variable
static, you’ll have a copy per translation unit – i.e. it’s not a global. To make it global, you need to declare itexternin the header and define it in a single implementation file.