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

6

我正在学习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;
}

1
为确保符号正确解析,请将Psapi.lib添加到TARGETLIBS并使用-DPSAPI_VERSION=1进行编译。你做了吗? - UmNyobe
2个回答

8
头文件声明函数的作用是让编译器编译您的代码。然而,链接器确实需要外部使用的函数的定义。这通常在导入库中提供。错误消息告诉您链接器没有这样的定义。
您必须包含与 psapi.h 文件对应的库。
#pragma comment( lib, "psapi.lib" )

编辑:: 从MSDN-Remarks Section中,

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

附加信息::

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

#pragma comment(lib, libname)告诉链接器将'libname'库添加到库依赖项列表中,就像你在项目属性的Input->Additional dependencies>中添加它一样。

请参见MSDN上的#pragma comment


根据MS关于GetProcessMemoryInfo函数的文档(http://msdn.microsoft.com/en-us/library/windows/desktop/ms683219%28v=vs.85%29.aspx),这也可能是“kernel32.lib”,具体取决于操作系统版本。 - jww

2

尝试添加这个

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

@user3471546 没问题。 :) - nemasu
根据MS关于GetProcessMemoryInfo函数的文档(http://msdn.microsoft.com/en-us/library/windows/desktop/ms683219%28v=vs.85%29.aspx),这也可能是“kernel32.lib”,具体取决于操作系统版本。 - jww

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接