Visual Studio C++ 与 psapi.lib 的链接 [英] Visual Studio C++ link with psapi.lib

查看:32
本文介绍了Visual Studio C++ 与 psapi.lib 的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 C 程序,它调用了在 psapi.h 中定义的函数 GetModuleInformation()

I have written a C Program which calls the function, GetModuleInformation() which is defined in psapi.h

我正在使用 Microsoft Visual Studio C++ 命令行编译器 (cl.exe) 来编译和链接程序.

I am using Microsoft Visual Studio C++ command line compiler (cl.exe) for compiling and linking the program.

我已经包含了 psapi.h 头文件:

I have included the psapi.h header file:

#include <psapi.h>

当我尝试编译时使用:

cl program.c

它生成目标文件,但是在链接阶段失败并出现错误:

It generates the object file, however fails during the linking stage with the error:

program.obj : error LNK2019: unresolved external symbol _GetModuleInformation@16 ref
erenced in function _main
program.exe : fatal error LNK1120: 1 unresolved externalsprogram.obj : error LNK2019: unresolved external symbol _GetModuleInformation@16 ref

我还将 psapi.lib 文件放在放置源代码文件 (program.c) 的同一文件夹中,但即使如此,我也会收到与上述相同的错误消息.

I also place the psapi.lib file in the same folder where the source code file (program.c) is placed, however even then I get the same error message as above.

如何使用命令行编译器 (cl.exe) 成功链接它?

How do I successfully link it using the command line compiler (cl.exe)?

推荐答案

方法一
如果你想从命令行编译 cl.exe 你可以使用 /link 选项来指定链接器选项:

Method 1
If you want to compile from the command line with cl.exe you can use the /link option to specify linker options :

cl /TC program.c /link psapi.lib

<小时>

方法二
以下 pragma 指令使链接器在链接时在源文件中搜索 psapi.lib 库.


Method 2
The following pragma directive causes the linker to search in your source file for the psapi.lib library while linking .

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

<小时>

错误的可能原因可能是,如果 psapi.lib 在链接器的附加库列表中丢失.
要解决此问题,请使用以下 /LIBPATH 选项:

cl /TC program.c /link Psapi.Lib /LIBPATH:C:\MyLibFolder\

其中 C:\MyLibFolder 指定文件夹的路径,其中包含您的 psapi.lib .

Where C:\MyLibFolder specifies a path to the folder, that contains your psapi.lib .

另外,您可以尝试设置适当的/SUBSYSTEM选项.
对于控制台应用程序使用:

Also, you can try to set the proper /SUBSYSTEM option .
For a Console application use :

/SUBSYSTEM:CONSOLE

类似问题的解决方案这里.

使用 GetModuleInformation 函数的示例:

#include <windows.h>
#include <stdio.h>    
#include <psapi.h>
#pragma comment( lib, "psapi.lib" )

int main(void)
{    
   MODULEINFO minfo = {0};  
   GetModuleInformation( GetCurrentProcess(), GetModuleHandle( "psapi.dll" ), &minfo, sizeof(minfo) );  
   /* printf("%X", minfo.lpBaseOfDll); /* The load address of the module */   
   return 0;
}

代码已经在 Windows 7 和 XP 上测试过.
链接会话的输出是:

The code has been tested on Windows 7 and XP .
The output from linking session is :

program.c

/out:program.exe 
psapi.lib 
/LIBPATH:C:\MyLibFolder\ 
/SUBSYSTEM:CONSOLE 
/VERBOSE 
program.obj 

Starting pass 1
Processed /DEFAULTLIB:uuid.lib
Processed /DEFAULTLIB:LIBCMT
Processed /DEFAULTLIB:OLDNAMES

Searching libraries
    Searching C:\MyLibFolder\psapi.lib:
      Found _GetModuleInformation@16
        Referenced in program.obj
        Loaded psapi.lib(PSAPI.DLL)
      Found __IMPORT_DESCRIPTOR_PSAPI
        Referenced in psapi.lib(PSAPI.DLL)
        Loaded psapi.lib(PSAPI.DLL)
      Found __NULL_IMPORT_DESCRIPTOR
        Referenced in psapi.lib(PSAPI.DLL)
        Loaded psapi.lib(PSAPI.DLL)
        ...

如果正确设置了 vsvars32.bat 和 Visual Studio 中所有适当的环境变量,上述链接器选项将生成有效的可执行 (.exe) 文件.

If vsvars32.bat and all appropriate environment variables in your Visual Studio are set correctly the above linker options will produce a valid executable(.exe) file.

这篇关于Visual Studio C++ 与 psapi.lib 的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆