debugging 未解析的外部符号__imp__free_dbg,__imp__malloc_dbg函数

r1zhe5dt  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(112)

很多问题似乎是重复的,但这不是。这是.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

Code:Toggle navigation

#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
k97glaaz

k97glaaz1#

我忘记了核心库总是需要链接。
只需添加核心库依赖项(名为$(CoreLibraryDependencies))或继承Additional Dependencies字段的默认值(例如,这是Visual Studio创建的选项)。
或者,这是需要链接的库列表(例如,发布版本):

  • msvcrt.lib
  • ucrt.lib
  • ...

相关问题