VC++ 中未解决的外部错误

Unresolved externals error in VC++

提问人:user3471546 提问时间:3/28/2014 最后编辑:Abhineetuser3471546 更新时间:3/28/2014 访问量:3918

问:

我正在学习 VC++ 并用代码检查内存的使用信息。这个程序给了我三个未解决的外部错误。

error LNK2019: unresolved external symbol _GetProcessMemoryInfo@12 referenced 
in function "void __cdecl PrintMemoryInfo(unsigned long)" 
(?PrintMemoryInfo@@YAXK@Z)

error LNK2019: unresolved external symbol _EnumProcesses@12 referenced in 
function _main

error LNK1120: 2 unresolved externals.

法典::

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <psapi.h>

// To ensure correct resolution of symbols, add Psapi.lib to TARGETLIBS
// and compile with -DPSAPI_VERSION=1

void PrintMemoryInfo( DWORD processID )
{
    HANDLE hProcess;
    PROCESS_MEMORY_COUNTERS pmc;

    // Print the process identifier.

    printf( "\nProcess ID: %u\n", processID );

    // Print information about the memory usage of the process.

    hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
                                    PROCESS_VM_READ,
                                    FALSE, processID );
    if (NULL == hProcess)
        return;

    if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
    {
        printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
        printf( "\tPeakWorkingSetSize: 0x%08X\n", 
                  pmc.PeakWorkingSetSize );
        printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
        printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaPeakPagedPoolUsage );
        printf( "\tQuotaPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaPagedPoolUsage );
        printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaPeakNonPagedPoolUsage );
        printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaNonPagedPoolUsage );
        printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage ); 
        printf( "\tPeakPagefileUsage: 0x%08X\n", 
                  pmc.PeakPagefileUsage );
    }

    CloseHandle( hProcess );
}

int main(void)
{
    // Get the list of process identifiers.

    DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;

    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
    {
        return 1;
    }

    // Calculate how many process identifiers were returned.

    cProcesses = cbNeeded / sizeof(DWORD);

    // Print the memory usage for each process

    for ( i = 0; i < cProcesses; i++ )
    {
        PrintMemoryInfo( aProcesses[i] );
    }

    return 0;
}
C visual-c++ lnk2019 未解析的外部

评论

1赞 UmNyobe 3/28/2014
To ensure correct resolution of symbols, add Psapi.lib to TARGETLIBS and compile with -DPSAPI_VERSION=1你这样做了吗?

答:

2赞 nemasu 3/28/2014 #1

尝试添加此内容

#pragma comment(lib, “psapi.lib”)

评论

0赞 jww 3/28/2014
这也可能是,具体取决于操作系统版本(根据有关 GetProcessMemoryInfo 函数的 MS 文档)。kernel32.lib
8赞 Abhineet 3/28/2014 #2

编译器使用声明函数的头文件来编译代码。但是,链接器确实需要对所使用的外部函数进行定义。这通常在导入库中提供。错误消息告诉您链接器没有这样的定义。

您必须包含相应的文件库。psapi.h

#pragma comment( lib, "psapi.lib" )

编辑:: 在 MSDN-Remarks 部分中,

若要确保符号的正确解析,请将 Psapi.lib 添加到 TARGETLIBS 宏并使用 -DPSAPI_VERSION=1 编译程序。

额外::

#pragma comment是一个编译器指令,指示 Visual C++ 在生成的对象文件中留下注释。然后,链接器在处理对象文件时可以读取注释。

#pragma comment(lib, libname)告诉链接器将“libname”库添加到库依赖项列表中,就像已将其添加到项目属性中一样Linker->Input->Additional dependencies

请参阅 #pragma MSDN上的评论

评论

0赞 jww 3/28/2014
这也可能是,具体取决于操作系统版本(根据有关 GetProcessMemoryInfo 函数的 MS 文档)。kernel32.lib