很多问题似乎是重复的,但事实并非如此。这是 .lib 链接错误,而不是 .obj 链接错误。
错误:
1>msvcprtd.lib(locale0_implib.obj) : error LNK2019: unresolved external symbol __imp__free_dbg referenced in function "public: static void __cdecl std::_Fac_node::operator delete(void *)" (??3_Fac_node@std@@SAXPEAX@Z)
1>msvcprtd.lib(locale0_implib.obj) : error LNK2019: unresolved external symbol __imp__malloc_dbg referenced in function "public: static void * __cdecl std::_Fac_node::operator new(unsigned __int64)" (??2_Fac_node@std@@SAPEAX_K@Z)
1>C:\Users\asdfs\source\repos\Debugger\x64\Debug\Debugger.exe : fatal error LNK1120: 2 unresolved externals
代码: 调试循环.h:
#pragma once
#include <windows.h>
#include <iostream>
#include <ostream>
DWORD OnCreateThreadDebugEvent(const LPDEBUG_EVENT);
DWORD OnCreateProcessDebugEvent(const LPDEBUG_EVENT);
DWORD OnExitThreadDebugEvent(const LPDEBUG_EVENT);
DWORD OnExitProcessDebugEvent(const LPDEBUG_EVENT);
DWORD OnLoadDllDebugEvent(const LPDEBUG_EVENT);
DWORD OnUnloadDllDebugEvent(const LPDEBUG_EVENT);
DWORD OnOutputDebugStringEvent(const LPDEBUG_EVENT);
DWORD OnRipEvent(const LPDEBUG_EVENT);
void EnterDebugLoop(const LPDEBUG_EVENT DebugEv);
main.cpp:
#include <iostream>
#include <windows.h>
#include <psapi.h>
#include "DebugLoop.h"
#pragma comment(lib, "Psapi.lib")
int main(int argc, char *argv[])
{
STARTUPINFOA si;
PROCESS_INFORMATION pi;
DEBUG_EVENT dbgEvent;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
ZeroMemory(&dbgEvent, sizeof(dbgEvent));
if (argc != 2)
{
printf("Usage: %s [cmdline]\n", argv[0]);
return 1;
}
// Start the child process.
if (!CreateProcessA(NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf("CreateProcess failed (%d).\n", GetLastError());
return 1;
}
// Wait until child process exits.
if (WaitForDebugEventEx(&dbgEvent, INFINITE) > 0)
EnterDebugLoop(&dbgEvent);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
命令行:
编译器:
/JMC /ifcOutput "x64\Release\"
/GS /W1 /Zc:wchar_t /ZI /Gm- /Od
/Fd"x64\Release\vc143.pdb"
/Zc:inline /fp:precise /D "_MBCS"
/errorReport:prompt /WX-
/Zc:forScope /RTC1 /Gd /MDd /FC
/Fa"x64\Release\" /EHsc
/Fo"x64\Release\" /FAcs
/Fp"x64\Release\Debugger.pch"
/diagnostics:column
链接器:
/OUT:"C:\Users\asdfs\source\repos\Debugger\x64\Release\Debugger.exe"
/MANIFEST /NXCOMPAT
/PDB:"C:\Users\asdfs\source\repos\Debugger\x64\Release\Debugger.pdb"
/DYNAMICBASE "msvcrtd.lib" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
/DEBUG:FULL /MACHINE:X64
/INCREMENTAL
/PGD:"C:\Users\asdfs\source\repos\Debugger\x64\Release\Debugger.pgd"
/SUBSYSTEM:CONSOLE
/MANIFESTUAC:"level='asInvoker' uiAccess='false'"
/ManifestFile:"x64\Release\Debugger.exe.intermediate.manifest"
/LTCGOUT:"x64\Release\Debugger.iobj"
/ERRORREPORT:PROMPT
/ILK:"x64\Release\Debugger.ilk"
/VERBOSE /TLBID:1
我忘记了始终需要链接的核心库。
只需添加核心库依赖项(名为
$(CoreLibraryDependencies)
)或继承 Additional Dependencies
字段的默认值(例如,该选项是由 Visual Studio 创建的)。
或者,这是需要链接的库的列表(例如,发布版本):