简体   繁体   English

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

[英]Unresolved externals error in VC++

i am learning vc++ and checking with code for usage information of memory. 我正在学习vc ++并使用代码检查内存的使用信息。 this program is giving me three errors of unresolved externals.. 这个程序给了我三个未解决的外部错误..

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.

Code:: 码::

#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;
}

The header file that declares the function is used by the compiler to compile your code. 声明函数的头文件由编译器用于编译代码。 The linker though does need a definition of the external functions that are used. 但链接器确实需要定义所使用的外部函数。 That is typically supplied in an import library. 这通常在导入库中提供。 The error message tells you that the linker has no such definition. 该错误消息告诉您链接器没有这样的定义。

You have to include the respective library for psapi.h file. 您必须包含psapi.h文件的相应库。

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

EDIT:: From the MSDN-Remarks Section , 编辑::来自MSDN-Remarks Section

To ensure correct resolution of symbols, add Psapi.lib to the TARGETLIBS macro and compile the program with -DPSAPI_VERSION=1 . 要确保正确解析符号,请将Psapi.lib添加到TARGETLIBS宏并使用-DPSAPI_VERSION = 1编译该程序。

Extra:: 额外::

#pragma comment is a compiler directive which indicates Visual C++ to leave a comment in the generated object file. #pragma comment是一个编译器指令,它指示Visual C ++在生成的目标文件中留下注释。 The comment can then be read by the linker when it processes object files. 然后,链接器在处理目标文件时可以读取注释。

#pragma comment(lib, libname) tells the linker to add the 'libname' library to the list of library dependencies, as if you had added it in the project properties at Linker->Input->Additional dependencies #pragma comment(lib, libname)告诉链接器将“libname”库添加到库依赖项列表中,就好像您已在Linker->Input->Additional dependencies的项目属性中添加它一样。

See #pragma comment on MSDN 请参阅MSDN上的#pragma评论

尝试添加此功能

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM