gpt4 book ai didi

c++ - VC++ 中 Unresolved external 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:52 26 4
gpt4 key购买 nike

我正在学习vc++,正在用代码查看内存的使用信息。这个程序给我三个 Unresolved external 错误..

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

最佳答案

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

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

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

编辑:来自MSDN-Remarks Section ,

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

额外::

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

#pragma comment(lib, libname) 告诉链接器将“libname”库添加到库依赖项列表中,就像您已将它添加到 Linker 的项目属性中一样->输入->附加依赖

参见 #pragma comment在 MSDN 上

关于c++ - VC++ 中 Unresolved external 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22708550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com