Visual Studio C++ link with psapi.lib

試著忘記壹切 提交于 2019-12-06 21:32:05

问题


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

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

I have included the psapi.h header file:

#include <psapi.h>

when I try to compile using:

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

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.

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


回答1:


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

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" )

Possible reason for your errors can be, if psapi.lib is missing in a list of additional libraries of linker.
To resolve this, use the following /LIBPATH option :

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

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

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

/SUBSYSTEM:CONSOLE

Solution to similar problem here .

Example on using the GetModuleInformation function :

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

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)
        ...

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.



来源:https://stackoverflow.com/questions/18432745/visual-studio-c-link-with-psapi-lib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!