namespace and global variable

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Natulux
I live to help wx-kind
I live to help wx-kind
Posts: 189
Joined: Thu Aug 03, 2017 12:20 pm

namespace and global variable

Post by Natulux » Wed Feb 06, 2019 12:48 pm

Hey guys,

I am a little confused with what might be a standard C++ issue, but I don't get it. There is much to read about namespaces and global variables, but I can't see the error in my case.

My app uses a function in global namespace, to write into a log file whenever I like to.
For that purpose, I implemented into the header of

myApp.h

Code: Select all

namespace Log
{
	extern bool writelogfile;
	extern wxString sLastLog;
	
	void SetWriteLogFile(bool bWriteLog);
	void DoLog(wxString logstring, bool bForceLog = false, bool bNoDoubleLog = false);
};
and

myApp.cpp

Code: Select all

namespace Log
{
	bool writelogfile = false;
	wxString sLastLog = "";

	void SetWriteLogFile(bool bWriteLog)
	{
		writelogfile = bWriteLog;
	}

	void Log::DoLog(wxString logstring, bool bForceLog, bool bNoDoubleLog)
	{
		if (bNoDoubleLog)
		{
			if (sLastLog == logstring)
			{
				return;
			}
		}

		bool bLogging = false;

		if (bForceLog)
		{
			bLogging = true;
		}
		else if (writelogfile)
		{
			bLogging = true;
		}

		if (bLogging)
		{
			sLastLog = logstring;
			wxLogNull logNo;
			wxFileName fn_logfile = wxStandardPaths::Get().GetExecutablePath();
			fn_logfile.SetExt("log");

			wxString datetime = wxDateTime::Now().FormatISOCombined();
			wxString buf = "";
			buf << datetime << " - " << logstring;

			wxTextFile file(fn_logfile.GetFullPath());
			if (!fn_logfile.FileExists())
			{
				file.Create();
			}
			if (file.Open())
			{
				while (file.GetLineCount()>500)
				{
					file.RemoveLine(0);
				}
				file.AddLine(buf);
				/*Datei speichern*/
				file.Write();
				file.Close();
			}
		}
	}
};
After this little log class, the real MyApp class follows. In the

Code: Select all

MyApp::OnInit
I already use the namespace to set the the global variable:

Code: Select all

Log::SetWriteLogFile(true);
Now I wanted to outsource that log namespace into a log.h and log.cpp to use it in other apps as well.
To use them, I loaded the files to my project source files and #include "log.h" in myApp.h, instead of declaring the namespace there. But if I do that, I run into several linker errors, complaining about unresolved symbols.

As a matter of fact, I already have another namespace outsourced to h/cpp just like this one, but it isn't used in the OnInit of the main class.
Is this the problem I might be facing here? (Is the #include resolved to late?) Or am I just color blind atm?

Thank you!
Natu
Attachments
CompilerLinkerError.png
CompilerLinkerError.png (20.94 KiB) Viewed 154 times

User avatar
doublemax
Moderator
Moderator
Posts: 13993
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: namespace and global variable

Post by doublemax » Thu Feb 07, 2019 12:13 am

Worked without errors for me when i created a log.h and log.cpp from the code you've shown (and added the missing includes for wx classes).
Use the source, Luke!

Natulux
I live to help wx-kind
I live to help wx-kind
Posts: 189
Joined: Thu Aug 03, 2017 12:20 pm

Re: namespace and global variable

Post by Natulux » Thu Feb 07, 2019 8:37 am

doublemax wrote:Worked without errors for me when i created a log.h and log.cpp from the code you've shown (and added the missing includes for wx classes).
Good to know, thanks for testing! When the code is right, I need to double check my usage of it.
Or maybe it just works now, because the sun is out.

Best
Natu

Post Reply