LINK2019 使用 /MTd 设置而不是 /MDd 重建库时出错
LINK2019 Error rebuilding library with /MTd setting instead of /MDd
我知道它已经被详细讨论过很多次了,但我的情况有些特殊,我不知道如何正确解决。
带有 /MDd 解决方案的库构建正常。但这不是应用程序需要的,因为它需要 /MT(d) 版本。
现在,我已将编译器选项更改为 /MTd,解决了一些外部项目依赖项,但仍然得到:
Error LNK2019 unresolved external symbol "bool __cdecl std::uncaught_exception(void)" (?uncaught_exception@std@@YA_NXZ) referenced in function "public: __cdecl std::basic_ostream<char,struct std::char_traits<char> >::sentry::~sentry(void)" (??1sentry@?$basic_ostream@DU?$char_traits@D@std@@@std@@QEAA@XZ) vcruntime140 ..\vcruntime140 ..\vcruntime140\log.obj
log.h就是这样:
#ifndef LOG_H
#define LOG_H
#include <string>
namespace hooks {
/** Prints message to the file only if debug mode setting is enabled. */
void logDebug(const std::string& logFile, const std::string& message);
/** Prints message to the file. */
void logError(const std::string& logFile, const std::string& message);
} // namespace hooks
#endif // LOG_H
log.cpp
#include "log.h"
#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
namespace hooks {
static void logAction(const std::string& logFile, const std::string& message)
{
using namespace std::chrono;
std::ofstream file(logFile.c_str(), std::ios_base::app);
const std::time_t time{std::time(nullptr)};
const std::tm tm = *std::localtime(&time);
file << "[" << std::put_time(&tm, "%c") << "] " << message << "\n";
}
void logDebug(const std::string& logFile, const std::string& message)
{
logAction(logFile, message);
}
void logError(const std::string& logFile, const std::string& message)
{
logAction(logFile, message);
}
} // namespace hooks
hooks.h
#ifndef HOOKS_H
#define HOOKS_H
#include <string>
#include <utility>
#include <vector>
namespace hooks {
using HookInfo = std::pair<void**, void*>;
using Hooks = std::vector<HookInfo>;
/** Returns array of hooks to setup. */
Hooks getHooks();
Hooks getVftableHooks();
} // namespace hooks
#endif // HOOKS_H
hooks.cpp
#include "hooks.h"
namespace hooks {
Hooks getHooks()
{
Hooks hooks;
return hooks;
}
Hooks getVftableHooks()
{
Hooks hooks;
return hooks;
}
} // namespace hooks
知道如何解决吗?
已找到解决方法,剩下的问题其实与上述无关。以上错误通过以下方式解决:
将 MSVCPRTD.LIB 添加到其他库 link 似乎可以消除 LINK2019 的问题。
问题的发生是因为该库中定义的某些标准库函数未加载。我通过检查错误消息和谷歌搜索缺少哪些函数定义以及它们属于哪个库来意识到这一点。
但现在 MSVCPRTD.LIB 有问题,因为根据 Microsoft 文档,它是一个动态库而不是静态库:https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features?view=msvc-160
MSVCPRTD.LIB 的问题可以通过加载 libcmtd.lib 来解决,它是相同库的 /MTd 版本。
我知道它已经被详细讨论过很多次了,但我的情况有些特殊,我不知道如何正确解决。
带有 /MDd 解决方案的库构建正常。但这不是应用程序需要的,因为它需要 /MT(d) 版本。
现在,我已将编译器选项更改为 /MTd,解决了一些外部项目依赖项,但仍然得到:
Error LNK2019 unresolved external symbol "bool __cdecl std::uncaught_exception(void)" (?uncaught_exception@std@@YA_NXZ) referenced in function "public: __cdecl std::basic_ostream<char,struct std::char_traits<char> >::sentry::~sentry(void)" (??1sentry@?$basic_ostream@DU?$char_traits@D@std@@@std@@QEAA@XZ) vcruntime140 ..\vcruntime140 ..\vcruntime140\log.obj
log.h就是这样:
#ifndef LOG_H
#define LOG_H
#include <string>
namespace hooks {
/** Prints message to the file only if debug mode setting is enabled. */
void logDebug(const std::string& logFile, const std::string& message);
/** Prints message to the file. */
void logError(const std::string& logFile, const std::string& message);
} // namespace hooks
#endif // LOG_H
log.cpp
#include "log.h"
#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
namespace hooks {
static void logAction(const std::string& logFile, const std::string& message)
{
using namespace std::chrono;
std::ofstream file(logFile.c_str(), std::ios_base::app);
const std::time_t time{std::time(nullptr)};
const std::tm tm = *std::localtime(&time);
file << "[" << std::put_time(&tm, "%c") << "] " << message << "\n";
}
void logDebug(const std::string& logFile, const std::string& message)
{
logAction(logFile, message);
}
void logError(const std::string& logFile, const std::string& message)
{
logAction(logFile, message);
}
} // namespace hooks
hooks.h
#ifndef HOOKS_H
#define HOOKS_H
#include <string>
#include <utility>
#include <vector>
namespace hooks {
using HookInfo = std::pair<void**, void*>;
using Hooks = std::vector<HookInfo>;
/** Returns array of hooks to setup. */
Hooks getHooks();
Hooks getVftableHooks();
} // namespace hooks
#endif // HOOKS_H
hooks.cpp
#include "hooks.h"
namespace hooks {
Hooks getHooks()
{
Hooks hooks;
return hooks;
}
Hooks getVftableHooks()
{
Hooks hooks;
return hooks;
}
} // namespace hooks
知道如何解决吗?
已找到解决方法,剩下的问题其实与上述无关。以上错误通过以下方式解决:
将 MSVCPRTD.LIB 添加到其他库 link 似乎可以消除 LINK2019 的问题。
问题的发生是因为该库中定义的某些标准库函数未加载。我通过检查错误消息和谷歌搜索缺少哪些函数定义以及它们属于哪个库来意识到这一点。
但现在 MSVCPRTD.LIB 有问题,因为根据 Microsoft 文档,它是一个动态库而不是静态库:https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features?view=msvc-160
MSVCPRTD.LIB 的问题可以通过加载 libcmtd.lib 来解决,它是相同库的 /MTd 版本。