0

So I have a really simple log class using spdlog.

The Log.h file looks as following:

#pragma once
#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_color_sinks.h"

namespace Engine {
    class Log
    {
    public:
        static void Init();
        static std::shared_ptr<spdlog::logger>& GetLogger() { return m_ELogger; };
    private:
        static std::shared_ptr<spdlog::logger> m_ELogger;
    };
}

#ifdef _DEBUG
    #define EN_ERROR(...) ::Engine::Log::GetLogger()->error(__VA_ARGS__)
    #define EN_WARN(...)  ::Engine::Log::GetLogger()->warn(__VA_ARGS__)
    #define EN_INFO(...)  ::Engine::Log::GetLogger()->info(__VA_ARGS__)
    #define EN_TRACE(...) ::Engine::Log::GetLogger()->trace(__VA_ARGS__)
#else 
    #define EN_ERROR(x) 
    #define EN_WARN(x)  
    #define EN_INFO(x)  
    #define EN_TRACE(x) 
#endif

and the Log.cpp file looks like this:

#include "Log.h"

namespace Engine {
    void Log::Init()
    {
        spdlog::set_pattern("[%H:%M:%S] [%n]: %v");

        m_ELogger = spdlog::stdout_color_mt("ENGINE");
        m_ELogger->set_level(spdlog::level::trace);
        m_ELogger->info("Logger initialized");
    }
}

And my calling code in a different file where Log.h is included looks like this:

int main() {
    Engine::Log::Init();
    int a = 55;
    EN_ERROR("asdf {0}", a);
}

It says unresolved external symbol: private: static class std::shared_ptr>class spdlog::logger> Engine::Log::m_ELogger. I know what this error means and researched quite a bit and also tried some things but I just cant get my head around why this isnt working. The declaration is there, the defition is there. Everything is there to make the linker happy.

Nicky
  • 121
  • 1
  • 7
  • m_ELogger needs to be defined in the cpp file as well, just as you have done with the static function. – Niall Jan 15 '21 at 14:56
  • This doesnt fix the problem for me unfortunately. I know that static means it gets internal linking and so on. But whatever. Guess I need to find out another way. – Nicky Jan 15 '21 at 19:16
  • Did the default initialise `std::shared_ptr Engine::Log::m_ELogger {}` not work? – Niall Jan 15 '21 at 19:21
  • No I wrote ``` static std::shared_ptr m_ELogger;``` in the .cpp file and it still does not work. Also tried different scopes but still nothing. – Nicky Jan 15 '21 at 19:43
  • When I type that line in the Log.cpp file I dont get an error. However I get the error no matter what in the EntryPoint.cpp file which is the file that contains my main() function. – Nicky Jan 15 '21 at 19:49
  • It looks like your defined another variable named the same, but different scopes so there is no compiler error. – Niall Jan 15 '21 at 19:54
  • How are you linking the two object files (you don’t mention toolset so typically .o or .obj)? – Niall Jan 15 '21 at 19:57
  • I am using Visual Studio so those are .obj files. Do I need to define a static variable in every .cpp file where I want to use it because of the internal linking? Because that sounds really stupid to me. Also I use static in a class context where it means something differetn than in a function context. So again I dont see what is wrong here. – Nicky Jan 15 '21 at 20:38
  • No, only define it was, each cpp file just needs to see its declaration. It still sounds like the definition is not correct. I suspect the scoping is just not quiet right in the definition. – Niall Jan 15 '21 at 20:44
  • Do you know what exactly is wrong with the scope? Because I cant see it. – Nicky Jan 15 '21 at 20:50
  • 1
    Oh nevermind I got it. I needed to change `static std::shared_ptr m_ELogger` to `static std::shared_ptr Log::m_ELogger`. – Nicky Jan 15 '21 at 20:52

0 Answers0