Ok I'm trying to get my head around g++ and librarys. I have a few files that I've compiled into a library
from the make file
$(CC) -fPIC -c -o $@ $< -O2 -D__PS2
then
$(CC) -shared -o $@ $(OBJ_FILES) -O2 -D__PS2
this compiles fine.
from the program that uses the lib
$(CC) -c -o $@ $< -I./
compiles fine
$(CC) -o $@ $(OBJ_FILES) I./ -Llib -mootPS2lib.so
Linking the obj's together and it goes BOOM!
ld: duplicate symbol Moot::loggerInstance() in object_files/foo.o and object_files/main.o
foo.hpp
//include guards
#include <Moot/Logger.hpp>
class Foo
{
public:
void show();
}
foo.cpp
#include "Foo.hpp"
void Foo::show() { Moot::loggerInstance().turnLoggerOn(true); }
the main.cpp only includes foo and calls the show method.
Thanks for anyhelp.
UPDATE I wasn't clean with the details of the question, sorry.
I've got this working on Windows by making it a static library.
Logger.hpp looks like this. * Its not quite finished so I know there are a few things missing. but it works.
#ifndef MOOT_LOGGER_HPP
#define MOOT_LOGGER_HPP
#include <Moot/Platform.hpp>
#include <Moot/Utilities.hpp>
#include <iostream>
#include <fstream>
#include <string>
namespace Moot
{
//! Outputs info to a text file. By default the logger is off.
//! On the PS2 cout is used to output info onto the screen.
class Logger
{
Logger()
{
std::ofstream logfile;
logfile.open ("logfile.txt", std::ios::trunc);
logfile << "LogFile - most recent at the bottom\n";
logfile << "-----------------------------------\n \n";
logfile.close();
m_isLoggerOn = false;
}
Logger(const Logger&);
Logger& operator=(const Logger&);
~Logger() {}
bool m_isLoggerOn;
template <typename T>
void logMessage(T type)
{
# if (MOOT_ON_PS2)
std::cout << type;
# else
std::ofstream logfile;
logfile.open ("logfile.txt", std::ios::in | std::ios::app);
logfile << type;
logfile.close();
#endif
}
//! Overiden << to allow you string together types.
template <typename T>
Logger& operator<< (T type)
{
if (m_isLoggerOn) logMessage(type);
return *this;
}
//! Overiden << to allow you string together types.
Logger& operator<< (std::wstring wideStr)
{
if (m_isLoggerOn) logMessage(Moot::Utilities::convertWstringToString(wideStr));
return *this;
}
public:
//! Instance of the logger.
static Logger& getInstance()
{
static Logger log;
return log;
}
//! Switch Logging off or on. It is set to off by default.
void turnLoggerOn(bool setLogger)
{
m_isLoggerOn = setLogger;
}
}; // Logger
Logger& loggerInstance()
{
return Logger::getInstance();
}
//! Convenience variables
namespace {
Logger& lStart = loggerInstance();
const char lEnd = '\n';
}
} // Moot
#endif