英文:
How to resolve LNK2001 in c++ projects
问题
I have two projects, the first one is a dynamic library project, lets say Log
project, and there is a file: Log.h
#pragma once
#include "Core.h"
#include "spdlog/spdlog.h"
#include <memory>
namespace Prunus {
class __declspec(dllexport) Log {
public:
static void Init();
inline static std::shared_ptr<spdlog::logger>& GetCoreLogger() { return s_CoreLogger; }
inline static std::shared_ptr<spdlog::logger>& GetClientLogger() { return s_ClientLogger; }
static std::shared_ptr<spdlog::logger> s_CoreLogger;
static std::shared_ptr<spdlog::logger> s_ClientLogger;
};
}
while in Log.cpp
, it is:
#include "Log.h"
#include "spdlog/sinks/stdout_color_sinks.h"
namespace Prunus {
std::shared_ptr<spdlog::logger> Log::s_CoreLogger;
std::shared_ptr<spdlog::logger> Log::s_ClientLogger;
void Log::Init() {
spdlog::set_pattern("%^[%T] %n: %v%$");
s_CoreLogger = spdlog::stdout_color_mt("PRUNUS");
s_CoreLogger->set_level(spdlog::level::trace);
s_ClientLogger = spdlog::stdout_color_mt("APP");
s_ClientLogger->set_level(spdlog::level::trace);
}
}
And I tried to invoke the Log::Init() in Log project itself, it can work. But when I reference it in another project which references the Log project, it throws an error like:
Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol "public: static class std::shared_ptr<class spdlog::logger> Prunus::Log::s_CoreLogger" (?s_CoreLogger@Log@Prunus@@2V?$shared_ptr@Vlogger@spdlog@@@std@@A) Sandbox F:\code\Prunus\Prunus\Sandbox\SandboxApplication.obj 1
Not sure whether there are additional settings I missed in Visual Studio 2022.
I tried to use the Log::Init() inside the Log project, it can work, but it cannot while referencing it outside the project. What I want is to reference it outside the Log project.
英文:
I have two projects, the first one is a dynamic library project, lets say Log
project, and there is a file:
Log.h
#pragma once
#include "Core.h"
#include "spdlog/spdlog.h"
#include <memory>
namespace Prunus{
class __declspec(dllexport) Log
{
public:
static void Init();
inline static std::shared_ptr<spdlog::logger>& GetCoreLogger() { return s_CoreLogger; }
inline static std::shared_ptr<spdlog::logger>& GetClientLogger() { return s_ClientLogger; }
static std::shared_ptr<spdlog::logger> s_CoreLogger;
static std::shared_ptr<spdlog::logger> s_ClientLogger;
};
}
while in Log.cpp
, it is:
#include "Log.h"
#include "spdlog/sinks/stdout_color_sinks.h"
namespace Prunus {
std::shared_ptr<spdlog::logger> Log::s_CoreLogger;
std::shared_ptr<spdlog::logger> Log::s_ClientLogger;
void Log::Init() {
spdlog::set_pattern("%^[%T] %n: %v%$");
s_CoreLogger = spdlog::stdout_color_mt("PRUNUS");
s_CoreLogger->set_level(spdlog::level::trace);
s_ClientLogger = spdlog::stdout_color_mt("APP");
s_ClientLogger->set_level(spdlog::level::trace);
}
}
And I tried to invoke the Log::Init() in Log project it self, it can work.
But when I reference it in another project which references on Log project, it throws error like:
Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol "public: static class std::shared_ptr<class spdlog::logger> Prunus::Log::s_CoreLogger" (?s_CoreLogger@Log@Prunus@@2V?$shared_ptr@Vlogger@spdlog@@@std@@A) Sandbox F:\code\Prunus\Prunus\Sandbox\SandboxApplication.obj 1
Not sure whether there are additional settings I missed in visual studio 2022
I tried to use the Log::Init() inside the Log project, it can work, but it can not while referencing outside the project. What I want is to reference it outside the Log project.
答案1
得分: 0
不要导出整个类,特别是如果它包含静态变量。
将函数声明与定义分开。
namespace Prunus {
class Log
{
public:
__declspec(dllexport) static void Init();
__declspec(dllexport) inline static std::shared_ptr<spdlog::logger>& GetCoreLogger();
__declspec(dllexport) inline static std::shared_ptr<spdlog::logger>& GetClientLogger();
private:
static std::shared_ptr<spdlog::logger> s_CoreLogger;
static std::shared_ptr<spdlog::logger> s_ClientLogger;
};
}
.cpp
namespace Prunus {
std::shared_ptr<spdlog::logger> Log::s_CoreLogger;
std::shared_ptr<spdlog::logger> Log::s_ClientLogger;
void Log::Init() {
// ...
// ...
}
inline std::shared_ptr<spdlog::logger>& Log::GetCoreLogger() { return Log::s_CoreLogger; }
inline std::shared_ptr<spdlog::logger>& Log::GetClientLogger() { return Log::s_CoreLogger; }
}
英文:
Do not export an entire class, especially if it contains static variables.
Separate function declaration from definition.
namespace Prunus {
class Log
{
public:
__declspec(dllexport) static void Init();
__declspec(dllexport) inline static std::shared_ptr<spdlog::logger>& GetCoreLogger();
__declspec(dllexport) inline static std::shared_ptr<spdlog::logger>& GetClientLogger();
private:
static std::shared_ptr<spdlog::logger> s_CoreLogger;
static std::shared_ptr<spdlog::logger> s_ClientLogger;
};
}
.cpp
namespace Prunus {
std::shared_ptr<spdlog::logger> Log::s_CoreLogger;
std::shared_ptr<spdlog::logger> Log::s_ClientLogger;
void Log::Init() {
...
...
}
inline std::shared_ptr<spdlog::logger>& Log::GetCoreLogger() { return Log::s_CoreLogger; }
inline std::shared_ptr<spdlog::logger>& Log::GetClientLogger() { return Log::s_CoreLogger; }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论