vc++中未解决的外部错误
Unresolved externals error in VC++
我正在学习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: %un", 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%08Xn", pmc.PageFaultCount );
printf( "tPeakWorkingSetSize: 0x%08Xn",
pmc.PeakWorkingSetSize );
printf( "tWorkingSetSize: 0x%08Xn", pmc.WorkingSetSize );
printf( "tQuotaPeakPagedPoolUsage: 0x%08Xn",
pmc.QuotaPeakPagedPoolUsage );
printf( "tQuotaPagedPoolUsage: 0x%08Xn",
pmc.QuotaPagedPoolUsage );
printf( "tQuotaPeakNonPagedPoolUsage: 0x%08Xn",
pmc.QuotaPeakNonPagedPoolUsage );
printf( "tQuotaNonPagedPoolUsage: 0x%08Xn",
pmc.QuotaNonPagedPoolUsage );
printf( "tPagefileUsage: 0x%08Xn", pmc.PagefileUsage );
printf( "tPeakPagefileUsage: 0x%08Xn",
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 -备注部分,
为了保证符号的正确分辨率,需要添加Psapi。Lib to the使用-DPSAPI_VERSION=1编译程序。
额外::
#pragma comment
是一个编译器指令,指示Visual c++在生成的目标文件中留下注释。当链接器处理目标文件时,可以读取注释。
#pragma comment(lib, libname)
告诉链接器将'libname'库添加到库依赖列表中,就像您在Linker->Input->Additional dependencies
的项目属性中添加了它一样
参见MSDN上的#pragma注释
尝试添加
#pragma comment(lib, “psapi.lib”)
相关文章:
- Visual Studio 2017 中的 QtMetaObject 未解决的外部错误
- 无法确定未解决的外部错误来自何处
- DLIB On visual studio 2015抛出一个未解决的外部错误
- 排除外部错误R6025-纯虚拟函数调用
- 如何在我的指针和阵列功能中修复未解决的外部错误-LNK2019和LNK1120
- 将外部错误代码映射到std :: error_condition
- 虚拟函数未解决的外部错误
- 非常简单的 x64 组装/C++应用程序中未解决的外部错误
- 链接到SFML的未解决的外部错误(SFML 2.2,Visual Studio Community 2013)
- 为什么我的代码会出现LNK2019未解决的外部错误
- 尝试重载=运算符时出现1个未解析的外部错误
- c++中链表代码中未解决的外部错误
- 操作员过载未解决的外部错误LNK1120、LNK2019
- 未解决的外部错误
- 模板上的算术运算符过载,导致无法解析的外部错误
- 致命错误LNK1120:1 个未解析的外部 & 错误 LNK2019:函数___tmainCRTStartup中引用_main未解析的外部符号
- 模板c++中未解决的外部错误
- C++程序使用 C 库标头将"this"识别为关键字。外部"C"错误?
- DLL LNK2019错误(未解析的外部错误__imp)
- 如何将DLL链接到我的主项目?(正在获取未解析的外部错误)