Visual c++ VC9.0中的单例代码链接器错误。在使用gcc编译的linux中运行良好

Visual c++ VC9.0中的单例代码链接器错误。在使用gcc编译的linux中运行良好,visual-c++,singleton,g++,Visual C++,Singleton,G++,我有一个简单的记录器,它是作为单例实现的。当我在linux中用g++编译并运行它时,它的工作方式与我想要的一样,但是当我在Visual Studio 9.0中用vc++编译它时,我会出现以下错误。有办法解决这个问题吗?我不介意改变logger类,但我希望避免改变它的调用方式 1>Linking... 1>loggerTest.obj : error LNK2005: "public: static class Logger * __cdecl Logger::getInstance(

我有一个简单的记录器,它是作为单例实现的。当我在linux中用g++编译并运行它时,它的工作方式与我想要的一样,但是当我在Visual Studio 9.0中用vc++编译它时,我会出现以下错误。有办法解决这个问题吗?我不介意改变logger类,但我希望避免改变它的调用方式

1>Linking...
1>loggerTest.obj : error LNK2005: "public: static class Logger * __cdecl Logger::getInstance(void)" (?getInstance@Logger@@SAPAV1@XZ) already defined in Logger.obj
1>loggerTest.obj : error LNK2005: "public: void __thiscall Logger::log(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?log@Logger@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Logger.obj
1>loggerTest.obj : error LNK2005: "public: void __thiscall Logger::closeLog(void)" (?closeLog@Logger@@QAEXXZ) already defined in Logger.obj
1>loggerTest.obj : error LNK2005: "private: static class Logger * Logger::_instance" (?_instance@Logger@@0PAV1@A) already defined in Logger.obj
1>Logger.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > Logger::_path" (?_path@Logger@@0V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A)
1>loggerTest.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > Logger::_path" (?_path@Logger@@0V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A)
1>Logger.obj : error LNK2001: unresolved external symbol "private: static class boost::mutex Logger::_mutex" (?_mutex@Logger@@0Vmutex@boost@@A)
1>loggerTest.obj : error LNK2001: unresolved external symbol "private: static class boost::mutex Logger::_mutex" (?_mutex@Logger@@0Vmutex@boost@@A)
1>Logger.obj : error LNK2001: unresolved external symbol "private: static class std::basic_ofstream<char,struct std::char_traits<char> > Logger::_log" (?_log@Logger@@0V?$basic_ofstream@DU?$char_traits@D@std@@@std@@A)
1>loggerTest.obj : error LNK2001: unresolved external symbol "private: static class std::basic_ofstream<char,struct std::char_traits<char> > Logger::_log" (?_log@Logger@@0V?$basic_ofstream@DU?$char_traits@D@std@@@std@@A)
1>链接。。。
1> loggerTest.obj:错误LNK2005:“公共:静态类记录器*uu cdecl记录器::getInstance(void)”(?getInstance@Logger@@SAPAV1@XZ)已在Logger.obj中定义
1> loggerTest.obj:错误LNK2005:“public:void u thiscall Logger::log(class std::basic_string const&)”(?log@Logger@@QAEXABV?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@@std@@@Z)已在Logger.obj中定义
1> loggerTest.obj:错误LNK2005:“public:void u thisgall Logger::closeLog(void)”(?closeLog@Logger@@qaexz)已在Logger.obj中定义
1> loggerTest.obj:错误LNK2005:“私有:静态类记录器*记录器::_实例”(_instance@Logger@@0PAV1@A)已在Logger.obj中定义
1> Logger.obj:错误LNK2001:未解析的外部符号“private:static class std::basic_字符串记录器::_path”(_path@Logger@@0V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@A)
1> loggerTest.obj:错误LNK2001:未解析的外部符号“private:static class std::basic_字符串记录器::_path”(_path@Logger@@0V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@A)
1> Logger.obj:错误LNK2001:未解析的外部符号“private:static class boost::mutex Logger::_mutex”(_mutex@Logger@@0Vmutex@boost@@(A)
1> loggerTest.obj:错误LNK2001:未解析的外部符号“private:static class boost::mutex Logger::_mutex”(_mutex@Logger@@0Vmutex@boost@@(A)
1> Logger.obj:错误LNK2001:未解析的外部符号“private:static class std::basic_of stream Logger::_log”(_log@Logger@@0V?$basic_ofstream@DU?$char_traits@D@std@@@std@@A)
1> loggerTest.obj:错误LNK2001:未解析的外部符号“private:static class std::basic_of stream Logger::_log”(_log@Logger@@0V?$basic_ofstream@DU?$char_traits@D@std@@@std@@A)
代码,三个文件Logger.h Logger.cpp test.cpp

#ifndef __LOGGER_CPP__
#define __LOGGER_CPP__
#include "Logger.h"

Logger* Logger::_instance = 0;

//string Logger::_path = "log";
//ofstream Logger::_log;
//boost::mutex Logger::_mutex;

Logger* Logger::getInstance(){
  { 
    boost::mutex::scoped_lock lock(_mutex);
    if(_instance == 0) {
      _instance = new Logger;
   _path = "log";
    }
  } //mutex
  return _instance;
}


void Logger::log(const std::string& msg){
  {
    boost::mutex::scoped_lock lock(_mutex);
    if(!_log.is_open()){
      _log.open(_path.c_str());
    }
    if(_log.is_open()){
    _log << msg.c_str() << std::endl;
    }
  }
}

void Logger::closeLog(){
  Logger::_log.close();
}

#endif
\ifndef\uuuu记录器\uCPP__
#定义记录器CPP__
#包括“Logger.h”
Logger*Logger::_实例=0;
//字符串记录器::_path=“log”;
//流记录器的类型::u log;
//互斥体记录器::u互斥体;
Logger*Logger::getInstance(){
{ 
boost::mutex::作用域锁定(mutex);
如果(_实例==0){
_实例=新记录器;
_path=“log”;
}
}//互斥
返回_实例;
}
void Logger::log(const std::string&msg){
{
boost::mutex::作用域锁定(mutex);
如果(!\u log.is\u open()){
_log.open(_path.c_str());
}
如果(_log.is_open()){

_log这是一个问题,因为您已经通过编译CPP多次定义了符号,然后还将其包含在test.CPP中……按照惯例,您应该只包含声明,而不是像以前那样包含定义

我很惊讶海湾合作委员会会允许一个人在这方面如此松懈

改变

#include "Logger.cpp"


试一下。

问题是因为您已经在test.cpp中执行了
\include“Logger.cpp”
而不是
\include“Logger.h”
。因此,Logger.cpp中的符号将被定义多次(一次用于转换单元Logger.cpp,一次用于test.cpp)。有多个包含保护没有帮助,因为它只在翻译单元内工作。

为什么要在test.cpp中包含Logger.cpp?我想应该包含“Logger.h”
。谢谢!就这些。
#include <iostream>
#include "Logger.cpp"
using namespace std;
int main(int argc, char *argv[])
{
  Logger* log = Logger::getInstance();
  log->log("hello world\n");
  return 0;
}
#include "Logger.cpp"
#include "Logger.h"